---
title: "两个线程如何顺序执行"
author: "lastsoul"
date: "2015-08-07T16:29:45Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2015-08-07T16:32:56Z"
id: 2233624
url: "https://lastsoul.iteye.com/blog/2233624"
---

先看下面的例子：

```java


public class MyThread1 extends Thread {
	@Override
	public void run() {
		for(int i=0;i<10;i++){
			System.out.println(i);
		}
		System.out.println();
	}

}
public class MyThread2 extends Thread {
	public MyThread2(String name) {
		super(name);
	}
	@Override
	public void run() {
		for(int i=0;i<10;i++){
			System.out.println(this.getName()"":"+i);
		}
		System.out.println();
	}

	public static void main(String[] args) {
		Thread t1=new MyThread1("t1");
		Thread t2=new MyThread2("t2");
		t1.start();
		t2.start();
	}
}


```


输出结果两个线程是交替执行的，而且每次执行结果也不一样。那么如何让这两个线程顺序执行呢？
方法一：在一个线程中调用另一个线程的join方法。MyThread1不动，MyThread2类代码如下：
```java
public class MyThread2 extends Thread {
	/**
	 * 
	 */
	public MyThread2(String name) {
		super(name);
	}
	/* (non-Javadoc)
	 * @see java.lang.Thread#run()
	 */
	@Override
	public void run() {
		[color=red]
Thread t1=new MyThread1("t1");
		try {
			t1.start();
			t1.join(10000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
[/color]		for(int i=0;i<10;i++){
			
			System.out.println(this.getName()+":"+i);
		}
		System.out.println();
	}

	public static void main(String[] args) throws InterruptedException {
		//Thread t1=new MyThread1("t1");
		Thread t2=new MyThread2("t2");
		//t1.start();
		
		
		t2.start();
		
		
	}
}


```

此方式可以实现顺序执行两个方法。
方法二：采用synchronized关键字，示例代码如下：

```java

public class MyThread1 extends Thread {
	private PrintI pi;
	public MyThread1(String name,PrintI pi) {
		super(name);//线程名称,如果不手动给,系统会给默认值
		this.pi=pi;
	}
	
	@Override
	public void run() {
		
		pi.execute(getName());
		System.out.println();
		
		try {
			sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

public class MyThread2 extends Thread {
	private PrintI pi;
	public MyThread2(String name,PrintI pi) {
		super(name);//线程名称,如果不手动给,系统会给默认值
		this.pi=pi;
	}
	
	@Override
	public  void run() {
		/*
		try {
			Thread t1=new MyThread1("t1",pi);
			t1.start();
			t1.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		*/
		pi.execute(getName());
		System.out.println();
	}

	public static void main(String[] args) throws InterruptedException {
		PrintI pi=new PrintI();
		Thread t2=new MyThread2("t2",pi);
		t2.start();
		Thread t1=new MyThread1("t1",pi);
		t1.start();
		
	}
}

public class PrintI {
	public synchronized void execute(String name) {
		for (int i = 0; i < 10; ++i) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(name+": " + i);
		}
	}

}

```

把打印动作交由PrintI 的对象来处理,将该对象加锁来实现顺序执行。
synchronized 具体用法 请参考：<http://www.cnblogs.com/mengdd/archive/2013/02/16/2913806.html>