Home >Java >javaTutorial >Sample code for several methods of orderly execution of Java multi-threads

Sample code for several methods of orderly execution of Java multi-threads

黄舟
黄舟Original
2017-03-18 10:19:081661browse

This article mainly introduces relevant information on the summary of several methods of orderly execution of Java multi-threads. Friends in need can refer to the following

Summary of several methods of orderly execution of Java multi-threads

Colleagues raised this issue accidentally and practiced two methods personally. Of course there are definitely more and better ways.

Method 1

import java.util.concurrent.atomic.AtomicInteger;

public class OrderedThread1 {
 
 static AtomicInteger count = new AtomicInteger(0);

 public static void main(String[] args) throws InterruptedException {
 Task task1 = new Task(count, 0);
 Task task2 = new Task(count, 1);
 Task task3 = new Task(count, 2);
 Thread thread1 = new Thread(task1);
 Thread thread2 = new Thread(task2);
 Thread thread3 = new Thread(task3);
 thread1.setDaemon(true);
 thread2.setDaemon(true);
 thread3.setDaemon(true);
 thread1.start();
 thread2.start();
 thread3.start();
 
 Thread.sleep(1 * 1000);
 }

}

class Task implements Runnable {
 
 private AtomicInteger count;
 private int order;
 
 public Task(AtomicInteger count, int order) {
 this.count = count;
 this.order = order;
 }

 @Override
 public void run() {
 while (true) {
  if (count.get() % 3 == order) {
  System.out.println(Thread.currentThread().getName() + " ===== "+ order);
  count.incrementAndGet();
  }
 }
 }
}

This method should be a more common solution. Use atomic increment to control thread admission order.

Method 2

public class OrderedThread2 {
 static Holder holder = new Holder();
 public static void main(String[] args) throws InterruptedException {
 
 Task1 task1 = new Task1(holder, 0);
 Task1 task2 = new Task1(holder, 1);
 Task1 task3 = new Task1(holder, 2);
 Thread thread1 = new Thread(task1);
 Thread thread2 = new Thread(task2);
 Thread thread3 = new Thread(task3);
 thread1.setDaemon(true);
 thread2.setDaemon(true);
 thread3.setDaemon(true);
 thread1.start();
 thread2.start();
 thread3.start();
 
 Thread.sleep(1 * 1000);
 

 }

}

class Task1 implements Runnable {
 
 Holder holder;
 int order;
 
 public Task1(Holder holder, int order) {
 this.holder = holder;
 this.order = order;
 }

 @Override
 public void run() {
 while (true) {
  if (holder.count % 3 == order) {
  System.out.println(Thread.currentThread().getName() + " ===== "+ order);
  holder.count ++;
  }
 }
// int i = 0;
// while(i ++ < 10000){
//  holder.count ++;
// }
 }
}
class Holder {
 volatile int count = 0;
}

Method 2 uses the volatile keyword. Let each thread get the latest count value. When one of the threads performs the ++ operation, the other two threads will get the latest value and check whether the admission conditions are met.

ps: volatile is not thread-safe. And the two have nothing to do with each other. Volatile variables do not keep copies in user threads, so they provide the latest value to all threads. But just imagine, if multiple threads update this variable simultaneously, the result will be obvious. The last update will overwrite all previous updates, causing thread insecurity. In method two, only one thread meets the admission condition at a time, so there are no concurrent updates to variables. Keeping volatile values ​​up-to-date is completely irrelevant to thread safety, so don't misuse volatile to implement concurrency control.

The above is the detailed content of Sample code for several methods of orderly execution of Java multi-threads. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn