让线程的执行变得有序

线程如何有序执行。
(1)使用共享变量的方式:代码如下
利用原子递增控制线程准入顺序
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();
  }
 }
 }
}
(2)利用volatile来控制线程顺序,其实和上述思想一样,Atomic和volatile都保证代表顺序的数字是线程安全的
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;
	}

(3)用Thread.join方法: 在父线程中 子线程调用join方法,会使得子线程运行完之后,父线程再运行
public class JoinTest {

public static void main(String[] args) {
final Thread join1 = new Thread(new Runnable() {


@Override
public void run() {
  System.out.println("join1");  
}  
}, "T1");

final Thread join2 = new Thread(new Runnable() {
@Override
public void run() {
 
try {
join1.join();//确定join1执行完毕
System.out.println("join2"); 
} catch (InterruptedException e) {
e.printStackTrace();
}
}  
}, "T2");
final Thread join3 = new Thread(new Runnable() {
@Override
public void run() {
try {
join2.join();//确定join2执行完毕
System.out.println("join3");  

} catch (InterruptedException e) {
e.printStackTrace();
}
}  
}, "T3");
join1.start();
join2.start();
join3.start();
}
}
(4)线程池   newSingleThreadExecutor  这个线程处理完一个任务后接着处理下一个任务

public class JoinTest1 {


public static void main(String[] args) {
final Thread join1 = new Thread(new Runnable() {


@Override
public void run() {
  System.out.println("join1");  
}  
}, "T1");

final Thread join2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("join2");  
}  
}, "T2");
final Thread join3 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("join3");  
}  
}, "T3");
   ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(join1);
        executor.submit(join2);
        executor.submit(join3);
        executor.shutdown();
}
}

以上四种方式,有好的方法需要继续跟踪

你可能感兴趣的:(Java,并发编程)