The following is a pen test from Ali P7.

1, the title

Write A program, start three threads, the ID of the three threads are respectively A, B, C, 3 threads alternately print integers from 1 to 100, Sample:

Thread1:1
Thread2:2
Thread3:3
Thread1:4
Thread2:5
Thread3:6
....
Thread3:99
Thread1:100
Copy the code

Can you think about it and see if you can do that?

2

The three threads are all running. How do you get them to print in order?

You can think of the three threads as three people (ABC), ABC three people holding hands to form A ring, and then all three of them are sitting there waiting for notification, waiting for notification from whom, waiting for notification from the previous person, B waiting for notification from A, C waiting for notification from B, A waiting for notification from C.

At first: Program to evoke A, A print, notify B, then A enter dormancy wait wake up the notice, the B turn to print, print after notice C, B B into dormancy wait wake up the notice, now turn to C print, print after notice. A, C C into dormancy wait wake up the notice, in this way, After each thread prints, it is responsible for waking up the next thread and then going to sleep itself.

There are two key technical points:

1. Block the thread

2. Call up threads

Java. Util. Concurrent. The locks. LockSupport class provides two static methods just to support these operations

1. Park () : blocks the current thread

Unpark (Thread Thread) : used to wake up blocked threads

3, the answer

The code is fairly simple.

import java.util.ArrayList; import java.util.List; import java.util.concurrent.locks.LockSupport; public class ThreadTest { static List<Thread> threadList = new ArrayList<>(); Static int threadSize = 3; Static int threadIndex = 0; Static int maxValue = 100; Static int curValue = 1; static int curValue = 1; Public static void main(String[] args) throws InterruptedException {// Create thread for (int I = 1; i <= threadSize; I++) {Thread Thread = new Thread(() -> {while (true) {// block the current Thread locksupport.park (); If (curValue <= maxValue) {system.out.println (thread.currentthread ().getname () + ":" + curValue++); } else { break; } // Call up the next thread locksupport.unpark (threadlist.get (++threadIndex % threadlist.size ())); } // Call up all threads threadList.forEach(LockSupport::unpark); }); thread.setName(String.format("Thread%d", i)); threadList.add(thread); } for (Thread Thread: threadList) {thread.start(); } // Call up the first thread locksupport.unpark (threadlist.get (0)); }}Copy the code

Recommended reading

Why alibaba’s programmer growth rate so fast, read their internal data I understand

The knowledge system required for a programmer to earn 50 million a year.

– Read the three main features of concurrent programming in hours

What you don’t know about violent recursive algorithms

Three things to watch ❤️

If you find this article helpful, I’d like to invite you to do three small favors for me:

Like, forward, have your “like and comment”, is the motivation of my creation.

Follow the public account “Java Doudi” to share original knowledge from time to time.

Also look forward to the follow-up article ing🚀