public class A implements Runnable { @Override public void run() { // TODO Auto-generated method stub System.out.println("A"); } } public class B implements Runnable { @Override public void run() { // TODO Auto-generated method stub System.out.println("B"); } } public class C implements Runnable { @Override public void run() { // TODO Auto-generated method stub System.out.println("C"); } } /** * 方法一: * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub Thread thread1 = new Thread(new A()); thread1.start(); thread1.join(); Thread thread2 = new Thread(new B()); thread2.start(); thread2.join(); Thread thread3 = new Thread(new C()); thread3.start(); }
Use join () method, etc. before a thread is finished, the next thread can execute
/ ** * Method two: * / @Test public void test2 () { // create only one thread of the thread pool ExecutorService Executors.newSingleThreadExecutor ExecutorService = (); ExecutorService.submit (new new A ()); ExecutorService.submit ( B new new ()); ExecutorService.submit (new new C ()); executorService.shutdown (); }
Only one thread to create a thread pool to ensure that all tasks are performed in a specified order
/** * 线程顺序执行类 * @author 15735400536 * */ public class StepOver { private volatile int count = 1; public void a() throws InterruptedException { synchronized (this) { while(count != 1) { wait(); } System.out.println("线程A"); count = 2; notifyAll(); // if(count == 1) { // System.out.println("线程A"); // count = 2; // //Thread.currentThread().notifyAll(); // notifyAll(); // }else { // //Thread.currentThread().wait(1000); // wait(); // } } } public void b() throws InterruptedException { synchronized (this) { while(count != 2) { wait(); } System.out.println("线程B"); count = 3; notifyAll(); // if(count == 2) { // System.out.println("线程B"); // count = 3; // //Thread.currentThread().notifyAll(); // notifyAll(); // }else { // //Thread.currentThread().wait(1000); // wait(); // } } } public void c() throws InterruptedException { synchronized (this) { while(count != 3) { wait(); } System.out.println("线程C"); count = 1; notifyAll(); // if(count == 3) { // System.out.println("线程C"); // count = 0; // //Thread.currentThread().notifyAll(); // notifyAll(); // }else { // //Thread.currentThread().wait(1000); // wait(); // } } } } public class ThreadA implements Runnable { private StepOver stepOver; public ThreadA(StepOver stepOver) { this.stepOver = stepOver; } @Override public void run() { // TODO Auto-generated method stub try { stepOver.a(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public class ThreadB implements Runnable { private StepOver stepOver; public ThreadB(StepOver stepOver) { this.stepOver = stepOver; } @Override public void run() { // TODO Auto-generated method stub try { stepOver.b(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public class ThreadC implements Runnable { private StepOver stepOver; public ThreadC(StepOver stepOver) { this.stepOver = stepOver; } @Override public void run() { // TODO Auto-generated method stub try { stepOver.c(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 方法三 */ @Test public void test3() { StepOver stepOver = new StepOver(); for(int i=0; i<10 ; i++) { Thread b = new Thread(new ThreadB(stepOver)); b.start(); Thread a = new Thread(new ThreadA(stepOver)); a.start(); Thread c = new Thread(new ThreadC(stepOver)); c.start(); } }