I’m Little Xia Lufei. Learning shapes our lives. Technology changes the world.
The article directories
-
- Problem description
- Problem analysis
- The final code
Problem description
Start three threads to sum the number starting with 0, requiring each thread to sum ten times, and output 30.
Problem analysis
First three threads need to be started (accidentally saying nonsense), and the body of the thread executes ten increments in a loop. Note, however, that the increment operation is not atomic, so lock or use atomic variables. The code is very simple, implement:
public class Main {
private static final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Main-%d").build();
private static final ExecutorService executorService = new ThreadPoolExecutor(3.3.100L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1024), threadFactory, new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws InterruptedException {
AtomicInteger num = new AtomicInteger(0);
for (int i = 0; i < 3; i++) {
executorService.submit(new Runnable() {
@Override
public void run(a) {
for (int i = 0; i < 10; i++) { num.getAndIncrement(); }}}); } System.out.println(num); }}Copy the code
When you do something like this and you hit run and you get zero, that’s natural, the main thread starts three threads, and then it executes output, and the three worker threads haven’t been incremented yet. At this point you might want to use the sleep method to let the main thread sleep for 100 milliseconds before printing the statement. Sure, it works, but don’t you think it’s a little low? And in real development, who uses sleep because sleep is so out of control? Just a question: How do you estimate sleep time? Is it confusing, because you have no idea how long the other thread is executing? And of course you say, just to be sure, I’m going to let it sleep as long as possible, okay, so I’m going to let the main thread sleep for a day and a night, and then I’m going to hit run, and everybody’s going to go back to sleep and come back tomorrow. Is there anything we can do about it? Of course, remember our famous CountDownLatch, which enables multiple threads to wait for each other. Concurrent Programming in Java (3) : Principles and Practices of CountDownLatch
The final code
public class Main {
private static final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Main-%d").build();
private static final ExecutorService executorService = new ThreadPoolExecutor(3.3.100L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1024), threadFactory, new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws InterruptedException {
AtomicInteger num = new AtomicInteger(0);
CountDownLatch countDownLatch = new CountDownLatch(3);
for (int i = 0; i < 3; i++) {
executorService.submit(new Runnable() {
@Override
public void run(a) {
for (int i = 0; i < 10; i++) { num.getAndIncrement(); } countDownLatch.countDown(); }}); } countDownLatch.await(); System.out.println(num); }}Copy the code