逐梦少年,看你能不能发现宝藏(17)

<= 10; i++) {System.out.println(Thread.currentThread().getName() + "\t" + i);}//3.通知B线程,number = 3;c3.signal();} catch (Exception e) {e.printStackTrace();} finally {lock.unlock();}}public void print15() {lock.lock();try {//1.判断,为了避免虚假唤醒,的用while进行判断while (number != 3) {c3.await();}//2.干活for (int i = 1; i <= 10; i++) {System.out.println(Thread.currentThread().getName() + "\t" + i);}//3.通知B线程,number = 1;c1.signal();} catch (Exception e) {e.printStackTrace();} finally {lock.unlock();}}}public class SyncAndReentrantLockDemo {public static void main(String[] args) {ShareResoure shareResoure = new ShareResoure();new Thread(() -> {for (int i = 1; i <= 10; i++) {shareResoure.print5();}}, "A").start();new Thread(() -> {for (int i = 1; i <= 10; i++) {shareResoure.print10();}}, "B").start();new Thread(() -> {for (int i = 1; i <= 10; i++) {shareResoure.print15();}}, "C").start();}}====================================================================A 1A 2A 3A 4A 5B 1B 2B 3B 4B 5B 6B 7B 8B 9B 10C 1C 2C 3C 4C 5C 6C 7C 8C 9C 10 。。。。。。。。。。。。。。。。。。。。
3.0版本的生产者消费者,根本不用让线程等待,唤醒,都是自动操作的,代码如下:
/** volatile/CAS/automicInteger/BlockQueue/线程交互/原子引用* */class MyResource {private volatile boolean FLAG = true;//默认开启,进行生产+消费private AtomicInteger atomicInteger = new AtomicInteger();BlockingQueue blockingQueue = null;//高手一定传接口 。一定不会传类public MyResource(BlockingQueue blockingQueue) {this.blockingQueue = blockingQueue;System.out.println(blockingQueue.getClass().getName());}public void myProd() throws Exception {String data = http://www.kingceram.com/post/null;boolean retValue;while (FLAG) {data = atomicInteger.incrementAndGet() +"";retValue = http://www.kingceram.com/post/blockingQueue.offer(data, 2L, TimeUnit.SECONDS);if (retValue) {System.out.println(Thread.currentThread().getName() +"\t 插入队列" + data + "成功");} else {System.out.println(Thread.currentThread().getName() + "\t 插入队列" + data + "失败");}TimeUnit.SECONDS.sleep(1);}System.out.println(Thread.currentThread().getName() + "\t 大老板叫停了,表示Flag = false,生产动作结束");}public void myConsumer() throws Exception {String result = null;while (FLAG) {result = blockingQueue.poll(2L, TimeUnit.SECONDS);if(null == result || "".equals(result)){FLAG = false;System.out.println(Thread.currentThread().getName()+"\t 超过2秒钟没有取到蛋糕,消费退出");return;}System.out.println(Thread.currentThread().getName() + "\t 消费队列蛋糕"+result+"成功");}}public void stop()throws Exception {FLAG = false;}}public class ProdConsumer_BlockQueueDemo {public static void main(String[] args) throws Exception {MyResource myResource = new MyResource(new ArrayBlockingQueue<>(10));new Thread(()->{System.out.println(Thread.currentThread().getName()+"\t 生产线程启动");try {myResource.myProd();} catch (Exception e) {e.printStackTrace();}},"Prod").start();new Thread(()->{System.out.println(Thread.currentThread().getName()+"\t 消费线程启动");try {myResource.myConsumer();System.out.println();System.out.println();} catch (Exception e) {e.printStackTrace();}},"Consumer").start();//暂停一会线程try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); }System.out.println();System.out.println();System.out.println();System.out.println("5秒钟时间到,大老板main线程叫停,活动结束");myResource.stop();}}
下面代码是接口的调用
class MyThread implements Runnable {@Overridepublic void run() {}}class MyThread2 implements Callable {@Overridepublic Integer call() throws Exception {System.out.println("**************************** come in");try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); }return 1024;}}/** java* 多线程中,第3种获取多线程的方式* */public class CallableDemo {public static void main(String[] args) throws ExecutionException, InterruptedException {//两个线程,一个main线程,一个AA线程 。FutureTask