package com.hs.example.base.multithread.day01; import java.util.linkedList; public class SynchronizedTest4{ final linkedList lists = new linkedList<>(); final int MAX_COUNT = 10; private int count = 0; public synchronized void put(T t) { System.out.println(Thread.currentThread().getName() + " put start..."); while (lists.size() == MAX_COUNT) { try { System.out.println(Thread.currentThread().getName() + " waiting..."); this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } lists.add(t); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } count++; this.notifyAll(); System.out.println(Thread.currentThread().getName() + " put end..."); } public synchronized T get() { System.out.println(Thread.currentThread().getName() + " get start..."); T t = null; while (lists.size() == 0) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } t = lists.removeFirst(); count--; this.notifyAll(); System.out.println(Thread.currentThread().getName() + " get end..."); return t; } public static void main(String[] args) { SynchronizedTest4 synchronizedTest4 = new SynchronizedTest4(); for (int i = 0; i < 20; i++) { Thread t = new Thread(() -> { synchronizedTest4.put("str"); }, "put thread" + i); t.start(); } for (int i = 0; i < 20; i++) { Thread t = new Thread(() -> { synchronizedTest4.get(); }, "get thread" + i); t.start(); } } }
put thread0 put start... put thread0 put end... get thread5 get start... get thread5 get end... get thread18 get start... get thread19 get start... get thread15 get start... get thread11 get start... get thread17 get start... get thread10 get start... get thread16 get start... get thread13 get start... get thread12 get start... get thread9 get start... get thread8 get start... get thread4 get start... get thread14 get start... get thread7 get start... get thread6 get start... get thread0 get start... get thread3 get start... get thread1 get start... get thread2 get start... put thread19 put start... put thread19 put end... put thread18 put start... put thread18 put end... put thread15 put start... put thread15 put end... put thread14 put start... put thread14 put end... put thread9 put start... put thread9 put end... put thread16 put start... put thread16 put end... put thread13 put start... put thread13 put end... put thread12 put start... put thread12 put end... put thread11 put start... put thread11 put end... put thread10 put start... put thread10 put end... put thread17 put start... put thread17 waiting... put thread7 put start... put thread7 waiting... put thread6 put start... put thread6 waiting... put thread8 put start... put thread8 waiting... put thread3 put start... put thread3 waiting... put thread5 put start... put thread5 waiting... put thread4 put start... put thread4 waiting... put thread2 put start... put thread2 waiting... put thread1 put start... put thread1 waiting... get thread2 get end... get thread1 get end... get thread3 get end... get thread0 get end... get thread6 get end... get thread7 get end... get thread14 get end... get thread4 get end... get thread8 get end... get thread9 get end... put thread1 put end... put thread2 put end... put thread4 put end... put thread5 put end... put thread3 put end... put thread8 put end... put thread6 put end... put thread7 put end... put thread17 put end... get thread18 get end... get thread19 get end... get thread15 get end... get thread11 get end... get thread17 get end... get thread10 get end... get thread16 get end... get thread13 get end... get thread12 get end...
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)