请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册
首页 社区 Java java 小题目

java 小题目

猿梦 2022-11-19 16:15:46
package util;import java.util.Collection;import java.util.Comparator;import java.util.Iterator;import java.util.List;import java.util.ListIterator;import java.util.Map;import java.util.Set;import java.util.SortedMap;import java.util.SortedSet;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReadWriteLock;import java.util.concurrent.locks.ReentrantReadWriteLock;/*** <p>* 操作集合的类,可以返回以读写锁实现的各类集合<br>* </p>**/public class CollectionUtils {private CollectionUtils() {}/*** <p>* 返回用读写锁实现的集合* </p>** @param collection*            基础集合* @return 同步集合*/public static <T> Collection<T> readWriteLockCollection(Collection<T> collection) {return new ReadWriteLockCollection<T>(collection);}/*** <p>* 返回用读写锁实现的List* </p>** @param list*            基础List* @return 同步List*/public static <T> List<T> readWriteLockList(List<T> list) {return new ReadWriteLockList<T>(list);}/*** <p>* 返回用读写锁实现的Set* </p>** @param set*            基础Set* @return 同步Set*/public static <T> Set<T> readWriteLockSet(Set<T> set) {return new ReadWriteLockSet<T>(set);}/*** <p>* 返回用读写锁实现的SortedSet* </p>** @param sortedSet*            基础SortedSet* @return 同步SortedSet*/public static <T> SortedSet<T> readWriteLockSortedSet(SortedSet<T> sortedSet) {return new ReadWriteLockSortedSet<T>(sortedSet);}/*** <p>* 返回用读写锁实现的Map* </p>** @param map*            基础Map* @return 同步Map*/public static <K, V> Map<K, V> readWriteLockMap(Map<K, V> map) {return new ReadWriteLockMap<K, V>(map);}/*** <p>* 返回用读写锁实现的SortedMap* </p>** @param sortedMap*            基础SortedMap* @return 同步SortedMap*/public static <K, V> SortedMap<K, V> readWriteLockSortedMap(SortedMap<K, V> sortedMap) {return new ReadWriteLockSortedMap<K, V>(sortedMap);}private static class ReadWriteLockCollection<T> implements Collection<T> {private Collection<T> collection;protected ReadWriteLock lock = new ReentrantReadWriteLock();private ReadWriteLockCollection(Collection<T> collection) {if (collection == null)throw new NullPointerException();this.collection = collection;}@Overridepublic boolean add(T e) {Lock lock = this.lock.writeLock();lock.lock();try {return collection.add(e);} finally {lock.unlock();}}@Overridepublic boolean addAll(Collection<? extends T> c) {Lock lock = this.lock.writeLock();lock.lock();try {return collection.addAll(c);} finally {lock.unlock();}}@Overridepublic void clear() {Lock lock = this.lock.writeLock();lock.lock();try {collection.clear();} finally {lock.unlock();}}@Overridepublic boolean contains(Object o) {Lock lock = this.lock.readLock();lock.lock();try {return collection.contains(o);} finally {lock.unlock();}}@Overridepublic boolean containsAll(Collection<?> c) {Lock lock = this.lock.readLock();lock.lock();try {return collection.containsAll(c);} finally {lock.unlock();}}@Overridepublic boolean isEmpty() {Lock lock = this.lock.readLock();lock.lock();try {return collection.isEmpty();} finally {lock.unlock();}}@Overridepublic Iterator<T> iterator() {return collection.iterator();}@Overridepublic boolean remove(Object o) {Lock lock = this.lock.writeLock();lock.lock();try {return collection.remove(o);} finally {lock.unlock();}}@Overridepublic boolean removeAll(Collection<?> c) {Lock lock = this.lock.writeLock();lock.lock();try {return collection.removeAll(c);} finally {lock.unlock();}}@Overridepublic boolean retainAll(Collection<?> c) {Lock lock = this.lock.writeLock();lock.lock();try {return collection.retainAll(c);} finally {lock.unlock();}}@Overridepublic int size() {Lock lock = this.lock.readLock();lock.lock();try {return collection.size();} finally {lock.unlock();}}@Overridepublic Object[] toArray() {Lock lock = this.lock.readLock();lock.lock();try {return collection.toArray();} finally {lock.unlock();}}@Overridepublic <E> E[] toArray(E[] a) {Lock lock = this.lock.readLock();lock.lock();try {return collection.toArray(a);} finally {lock.unlock();}}}private static class ReadWriteLockList<T> extendsReadWriteLockCollection<T> implements List<T> {private List<T> list;private ReadWriteLockList(List<T> list) {super(list);this.list = list;}@Overridepublic void add(int index, T element) {Lock lock = this.lock.writeLock();lock.lock();try {list.add(index, element);} finally {lock.unlock();}}@Overridepublic boolean addAll(int index, Collection<? extends T> c) {Lock lock = this.lock.writeLock();lock.lock();try {return list.addAll(index, c);} finally {lock.unlock();}}@Overridepublic T get(int index) {Lock lock = this.lock.readLock();lock.lock();try {return list.get(index);} finally {lock.unlock();}}@Overridepublic int indexOf(Object o) {Lock lock = this.lock.readLock();lock.lock();try {return list.indexOf(o);} finally {lock.unlock();}}@Overridepublic int lastIndexOf(Object o) {Lock lock = this.lock.readLock();lock.lock();try {return stIndexOf(o);} finally {lock.unlock();}}@Overridepublic ListIterator<T> listIterator() {return list.listIterator();}@Overridepublic ListIterator<T> listIterator(int index) {return list.listIterator(index);}@Overridepublic T remove(int index) {Lock lock = this.lock.writeLock();lock.lock();try {return list.remove(index);} finally {lock.unlock();}}@Overridepublic T set(int index, T element) {Lock lock = this.lock.writeLock();lock.lock();try {return list.set(index, element);} finally {lock.unlock();}}@Overridepublic List<T> subList(int fromIndex, int toIndex) {Lock lock = this.lock.readLock();lock.lock();try {return new ReadWriteLockList<T>(list.subList(fromIndex, toIndex));} finally {lock.unlock();}}}private static class ReadWriteLockSet<T> extends ReadWriteLockCollection<T>implements Set<T> {private ReadWriteLockSet(Set<T> set) {super(set);}}private static class ReadWriteLockSortedSet<T> extends ReadWriteLockSet<T>implements SortedSet<T> {private SortedSet<T> sortedSet;private ReadWriteLockSortedSet(SortedSet<T> sortedSet) {super(sortedSet);this.sortedSet = sortedSet;}@Overridepublic Comparator<? super T> comparator() {Lock lock = this.lock.readLock();lock.lock();try {return parator();} finally {lock.unlock();}}@Overridepublic T first() {Lock lock = this.lock.readLock();lock.lock();try {return sortedSet.first();} finally {lock.unlock();}}@Overridepublic SortedSet<T> headSet(T toElement) {Lock lock = this.lock.readLock();lock.lock();try {return new ReadWriteLockSortedSet<T>(sortedSet.headSet(toElement));} finally {lock.unlock();}}@Overridepublic T last() {Lock lock = this.lock.readLock();lock.lock();try {return st();} finally {lock.unlock();}}@Overridepublic SortedSet<T> subSet(T fromElement, T toElement) {Lock lock = this.lock.readLock();lock.lock();try {return new ReadWriteLockSortedSet<T>(sortedSet.subSet(fromElement, toElement));} finally {lock.unlock();}}@Overridepublic SortedSet<T> tailSet(T fromElement) {Lock lock = this.lock.readLock();lock.lock();try {return new ReadWriteLockSortedSet<T>(sortedSet.tailSet(fromElement));} finally {lock.unlock();}}}private static class ReadWriteLockMap<K, V> implements Map<K, V> {private Map<K, V> map;protected ReadWriteLock lock = new ReentrantReadWriteLock();private ReadWriteLockMap(Map<K, V> map) {if (map == null)throw new NullPointerException();this.map = map;}@Overridepublic void clear() {Lock lock = this.lock.writeLock();lock.lock();try {map.clear();} finally {lock.unlock();}}@Overridepublic boolean containsKey(Object key) {Lock lock = this.lock.readLock();lock.lock();try {return map.containsKey(key);} finally {lock.unlock();}}@Overridepublic boolean containsValue(Object value) {Lock lock = this.lock.readLock();lock.lock();try {return map.containsValue(value);} finally {lock.unlock();}}@Overridepublic Set<java.util.Map.Entry<K, V>> entrySet() {Lock lock = this.lock.readLock();lock.lock();try {return new ReadWriteLockSet<java.util.Map.Entry<K, V>>(map.entrySet());} finally {lock.unlock();}}@Overridepublic V get(Object key) {Lock lock = this.lock.readLock();lock.lock();try {return map.get(key);} finally {lock.unlock();}}@Overridepublic boolean isEmpty() {Lock lock = this.lock.readLock();lock.lock();try {return map.isEmpty();} finally {lock.unlock();}}@Overridepublic Set<K> keySet() {Lock lock = this.lock.readLock();lock.lock();try {return new ReadWriteLockSet<K>(map.keySet());} finally {lock.unlock();}}@Overridepublic V put(K key, V value) {Lock lock = this.lock.writeLock();lock.lock();try {return map.put(key, value);} finally {lock.unlock();}}@Overridepublic void putAll(Map<? extends K, ? extends V> m) {Lock lock = this.lock.writeLock();lock.lock();try {map.putAll(m);} finally {lock.unlock();}}@Overridepublic V remove(Object key) {Lock lock = this.lock.writeLock();lock.lock();try {return map.remove(key);} finally {lock.unlock();}}@Overridepublic int size() {Lock lock = this.lock.readLock();lock.lock();try {return map.size();} finally {lock.unlock();}}@Overridepublic Collection<V> values() {Lock lock = this.lock.readLock();lock.lock();try {return new ReadWriteLockCollection<V>(map.values());} finally {lock.unlock();}}}private static class ReadWriteLockSortedMap<K, V> extendsReadWriteLockMap<K, V> implements SortedMap<K, V> {private SortedMap<K, V> sortedMap;private ReadWriteLockSortedMap(SortedMap<K, V> sortedMap) {super(sortedMap);this.sortedMap = sortedMap;}@Overridepublic Comparator<? super K> comparator() {Lock lock = this.lock.readLock();lock.lock();try {return parator();} finally {lock.unlock();}}@Overridepublic K firstKey() {Lock lock = this.lock.readLock();lock.lock();try {return sortedMap.firstKey();} finally {lock.unlock();}}@Overridepublic SortedMap<K, V> headMap(K toKey) {Lock lock = this.lock.readLock();lock.lock();try {return new ReadWriteLockSortedMap<K, V>(sortedMap.headMap(toKey));} finally {lock.unlock();}}@Overridepublic K lastKey() {Lock lock = this.lock.readLock();lock.lock();try {return stKey();} finally {lock.unlock();}}@Overridepublic SortedMap<K, V> subMap(K fromKey, K toKey) {Lock lock = this.lock.readLock();lock.lock();try {return new ReadWriteLockSortedMap<K, V>(sortedMap.subMap(fromKey, toKey));} finally {lock.unlock();}}@Overridepublic SortedMap<K, V> tailMap(K fromKey) {Lock lock = this.lock.readLock();lock.lock();try {return new ReadWriteLockSortedMap<K, V>(sortedMap.tailMap(fromKey));} finally {lock.unlock();}}}}
吾谢香柳交上#鄙人你们拿出来—我的这个就是一个写入,一个移除,写最多写十个进去,不知道是不是你的意思import java.util.ArrayList;public class B {public static void main(String[] args) throws Exception {Test add = new Test( &quot;add&quot;);Test del=new Test(&quot;del&quot;);add.start();del.start();}}class Test extends Thread {private static ArrayList list=new ArrayList();private int i=0;public Test( String name) {super(name);}@Overridepublic void run() {while(true){synchronized (list) {try {if(currentThread().getName().equals(&quot;add&quot;)){if(list.size()<10){list.add(++i);System.out.println(&quot; add 放入 &quot;+i);if(i>2){i=0;}list.notifyAll();}else{list.wait();}}else if(currentThread().getName().equals(&quot;del&quot;)){if(list.size()>0){System.out.println(&quot;del 得到 &quot;+list.get(list.size()-1).toString());list.remove(list.size()-1);list.notifyAll();}else{list.wait();}}Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}}}}}
朕小红送来#本王椅子抬高%不知符合你的要求不。。import java.util.ArrayList;import java.util.List;public class ThreadList {private static List<String> list = new ArrayList<String>();public static void main(String[] args) {for(int i = 0; i < 10; i++) {add(i + &quot;个&quot;);}for(int i = 0; i < 10; i++) {System.out.println(read(i));}}public static void add(String str) {synchronized(list) {list.add(str);}}public static String read(int i) {synchronized(list) {return list.get(i);}}}
狗你缩回去?本大人汤从阳交上#付费内容限时免费查看回答稍等一下哈需要些时间提问好的回答package lxday03;import java.util.Scanner;public class Student {int no;String name;double score;static double sum;int count;public Student(){public void input(){Scanner sc= new Scanner(System.in);System.out.println("请输入存放同学的位数为?人");count = sc.nextInt();System.out.printLn("请输入编号");no = sc.nextInt();System.out.println("请输入名字");name = sc.next();System.out.println("请输入成绩");score = sc.nextDouble();sum = sum+score;}double ave() {return sum/count; }public void showInfo(){System.out.println("该同学编号为"+no);System.out.println("该同学名字为"+name);System.out.println("该同学成绩为"+score);System.out.println("截至该同学目前的平均成绩为"+ave());    }}package lxday03;public class Student0 {public static void main(String args[]){System.out.println("请存放三个学生的信息");Student one = new Student();Student two = new Student();Student three = new Student();one.input();one.showInfo();two.input();two.showInfo();three.input();three.showInfo();  }}您参考一下这个网址代码吧,可能我打的符号会出错-cjn/p/.html可以说是一模一样希望我的回答对您有所帮助,祝愿您万事顺利,心想事成!祝您生活愉快!更多8条
老子曹觅松推倒,电视丁幻丝贴上¥/*** <p>Title: 先进先出队列</p>* <p>Description: </p>* <p>Copyright: Copyright (c) 2009</p>* <p>Company: </p>* @author wangyou* @version 2.0*/import java.util.Iterator;import java.util.LinkedList;import java.util.List;public class FIFOMsgQueue<T> {private final int DEFAULT_CAPABILITY =;private int capability;private LinkedList<T> msgQueue = new LinkedList<T>();private Object lock = new Object();/*** 默认的构造方法,其生成的队列的容量大小为默认的*/public FIFOMsgQueue() {this.capability = DEFAULT_CAPABILITY;}/*** 生成指定容量大小的队列** @param capability*            队列容量大小*/public FIFOMsgQueue(int capability) {this.capability = capability;}/*** 判断队列是否已满** @return*/public boolean isFull() {return (msgQueue.size() == capability ? true : false);}/*** 返回队列的容量大小** @return*/public int capability() {return capability;}/*** 返回当前队列大小** @return*/public int size() {return msgQueue.size();}/*** 返回当前队列是否为空** @return*/public boolean isEmpty() {return (msgQueue.size() <= 0 ? true : false);}/*** 将指定内容元素message放入队列中** @param message*            指定放入队列中的内容元素*/public void push(T message) {synchronized (lock) {if (isFull()) {try {lock.wait();} catch (InterruptedException e) {e.printStackTrace();}}msgQueue.addLast(message);}}/*** 取出队列中第一个内容元素** @return 队列中第一个内容元素*/public T pop() {synchronized (lock) {T message = null;if (!isEmpty()) {message = (T) msgQueue.removeFirst();lock.notifyAll();}return message;}}/*** 取出队列中指定个数的内容元素** @param num*            取出队列中的内容元素个数* @return*/public List<T> pop(int num) {synchronized (lock) {LinkedList<T> messages = new LinkedList<T>();for (int i = 0; i < num; i++) {if (!isEmpty()) {messages.addLast(this.pop());} else {break;}}lock.notifyAll();return messages;}}/*** 返回指定队列索引处的内容元素** @param index*            队列内容元素索引* @return 指定索引处的内容元素*/public T get(int index) {T message = null;if (isEmpty()) {message = (T) msgQueue.get(index);}return message;}/*** 移除队列指定索引处的内容元素** @param index*            队列内容元素索引* @return 移除的内容元素*/public T remove(int index) {synchronized (lock) {T message = null;if (!isEmpty()) {message = (T) msgQueue.remove(index);lock.notifyAll();}return message;}}/*** 获取队列迭代器** @return 队列迭代器*/public Iterator<T> iterator() {return msgQueue.iterator();}}

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册