Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does java realize that two threads output 1-100 sequentially alternately?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "java how to achieve the alternate output of two threads in order 1-100", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "java how to achieve the alternate output of two threads in order of 1-100" bar!

Solution one

With the above ideas, you are sure to quickly write the following code:

Public class PrintNumber extends Thread {private static int cnt = 0; private int id; / / thread number public PrintNumber (int id) {this.id = id;} @ Override public void run () {while (cnt < 100) {while (cnt%2 = = id) {cnt++; System.out.println ("thread_" + id + "num:" + cnt) }} public static void main (String [] args) {Thread thread0 = new PrintNumber (0); Thread thread1 = new PrintNumber (1); thread0.start (); thread1.start ();}}

But when you actually run it, you will find it!

Thread_0 num:1thread_0 num:3thread_1 num:3thread_1 num:5thread_1 num:6thread_0 num:5thread_0 num:8thread_0 num:9thread_1 num:8thread_0 num:11thread_1 num:11.

Not only in the wrong order, but also repeated and lost! What's the problem? Go back to the code cnt++; System.out.println ("thread_" + id + "num:" + cnt); these two lines, which mainly contain two actions, cnt++ and output, may have triggered the output of another thread when the cnt++ execution is complete. To simplify the execution process, JVM has four actions to perform at each moment.

Thread_0 cnt++

Thread_0 print

Thread_1 cnt++

Thread_1 print according to the semantics of Java as-if-serial, jvm only guarantees the order within a single thread, not between multiple threads, so the execution order of the above four steps may be 1234, 1324, or more likely 1342. For the above code, the final order may change. In addition, cnt++ can be disassembled into two lines of underlying instructions, tmp = cnt+ 1; cnt = tmp. When two threads execute the above instructions at the same time, they will face the same problem as step 1, 2, 3, 4. Yes, multithreaded behavior is as elusive as your girlfriend's mind. How to solve this problem? The essence of the solution is to ensure that the code executes as smoothly as we expected, and the correct solution is essentially the same principle as the following solutions, but in a different way.

The correct code for solution 1 is as follows:

Public class PrintNumber extends Thread {private static AtomicInteger cnt = new AtomicInteger (); private int id; public PrintNumber (int id) {this.id = id;} @ Override public void run () {while (cnt.get ())

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report