In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you the solution to the problem of java multithreading, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
See a multi-threaded topic on iteye, with reference to the implementation of netizens, then Eclipse debugging passed, can be regarded as a general understanding of the concurrent library of JAVA5, share it, welcome the students in the park to beat bricks.
Title:
It is required to use three threads to print 1, 2, 3, 4, 5 in order. 71,72,73,74, 75.
Thread 1 prints 1, 2, 3, 4, 5, 2, 6, 7, 8, 9, 10, and then thread 3 prints 11, 12, 13, 14, 15. Then thread 1 prints 16, 17, 18, 19, 20. And so on until thread 3 prints to 75.
Analysis: I feel that the person who asks the question is to see if you can control the multithreading well and let them proceed in an orderly manner.
1. Thread pool: 3 threads, which need to use the thread pool of the concurrent library
2. Lcok: when printing, only one thread is allowed to enter, while other threads wait
The following is the main code:
Import java.util.HashMap; import java.util.Map; import java.util.concurrent.CountDownLatch; 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; public class NumberPrinter {private Lock lock = new ReentrantLock (); private Condition C1 = lock.newCondition (); private Condition c2 = lock.newCondition () Private Condition c3 = lock.newCondition (); private Map condtionContext = new HashMap (); public NumberPrinter () {condtionContext.put (Integer.valueOf (0), C1); condtionContext.put (Integer.valueOf (1), c2); condtionContext.put (Integer.valueOf (2), c3);} private int count = 0 Public void print (int id) {lock.lock (); try {while (count*5 < 75) {int curID = calcID (); if (id = = curID) {for (int I = 1; I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.