<= 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
- 全球销量最好的100款手机。看看你用过的手机排在第几位? 世界之最的五部手机是什么
- 天津青少年篮球培训
- 海淀少年宫
- 宅多少年后就不算了,自的房子算不算宅
- 从“不良少年”到美国总统,奥巴马是如何走向权力巅峰 历史美国领导人之最
- 初中生四刷世界记录,“光速少年”再创佳绩 吉尼斯记录画报
- 十大国产经典网游,引领一代人的回忆,来看看你玩过几款! 中国十大网络游戏
- “同年不同脸”的9对明星,有人鲜嫩似少年,有人苍老如大爷 秦海璐吉尼斯记录
- 闸北区青少年活动中心
- 成都市青少年宫