本篇文章为大家展示了如何实现一个多线程,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
传统线程测试:* 子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程又循环100次,如此循环50次,请写出程序。
package com.lau.javabase.lock; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 传统线程测试: * 子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程又循环100次,如此循环50次,请写出程序。 */ public class TraditionThreadExerciseTest { volatile Boolean executeInTurn = true; public synchronized void subTask(int times) throws Exception { while(!executeInTurn){ this.wait(); } for(int i = 1; i <= 10; i++){ System.out.println("子任务内部运行第" + i + "次," + "总第" + times + "次"); } executeInTurn = false; this.notify(); } public synchronized void mainTask(int times) throws Exception { while(executeInTurn){ this.wait(); } for(int i = 1; i <= 100; i++){ System.out.println("主任务内部运行第" + i + "次," + "总第" + times + "次"); } executeInTurn = true; this.notify(); } /** * @Description:特别注意:不能将主线程和子线程循环50次的调用写在一个for循环里, * 因为这样会导致后面的跑到前面去,引发错序打印问题 * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { TraditionThreadExerciseTest test = new TraditionThreadExerciseTest(); ExecutorService threadPool = Executors.newFixedThreadPool(4); threadPool.submit(() -> { try { for(int i = 1; i <= 50; i++) { test.subTask(i); } } catch (Exception e) { e.printStackTrace(); } }, "subThread"); threadPool.submit(() -> { try { for(int i = 1; i <= 50; i++) { test.mainTask(i); } } catch (Exception e) { e.printStackTrace(); } }, "mainThread"); threadPool.shutdown(); } }
package com.lau.javabase.lock; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 传统线程测试: * 子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程又循环100次,如此循环50次,请写出程序。 */ public class TraditionThreadLockTest { volatile Boolean executeInTurn = true; Lock lock = new ReentrantLock(); Condition condition = lock.newCondition(); public void subTask(int times) throws Exception { try{ lock.lock(); while(!executeInTurn){ condition.await(); } for(int i = 1; i <= 10; i++){ System.out.println("子任务内部运行第" + i + "次," + "总第" + times + "次"); } executeInTurn = false; condition.signal(); } finally { lock.unlock(); } } public void mainTask(int times) throws Exception { try{ lock.lock(); while(executeInTurn){ condition.await(); } for(int i = 1; i <= 100; i++){ System.out.println("主任务内部运行第" + i + "次," + "总第" + times + "次"); } executeInTurn = true; condition.signal(); } finally { lock.unlock(); } } /** * @Description:特别注意:不能将主线程和子线程循环50次的调用写在一个for循环里, * 因为这样会导致后面的跑到前面去,引发错序打印问题 * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { TraditionThreadLockTest test = new TraditionThreadLockTest(); ExecutorService threadPool = Executors.newFixedThreadPool(4); threadPool.submit(() -> { try { for(int i = 1; i <= 50; i++) { test.subTask(i); } } catch (Exception e) { e.printStackTrace(); } }, "subThread"); threadPool.submit(() -> { try { for(int i = 1; i <= 50; i++) { test.mainTask(i); } } catch (Exception e) { e.printStackTrace(); } }, "mainThread"); threadPool.shutdown(); } }
上述内容就是如何实现一个多线程,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。