扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
本篇文章为大家展示了如何实现一个多线程,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
创新互联长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为昔阳企业提供专业的网站设计、成都网站建设,昔阳网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。
传统线程测试:* 子线程循环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(); } }
上述内容就是如何实现一个多线程,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流