---
title: "线程 第六讲 091212B"
author: "atomcow"
date: "2009-12-24T17:02:35Z"
tags: []
categories: ["编程语言"]
slug: "091212b"
draft: false
lastmod: "2009-12-24T17:02:35Z"
id: 555929
url: "https://atomcow.iteye.com/blog/555929"
---

```java
public class TestThread extends Thread {

	public static void main(String[] args) {
		TestThread tt=new TestThread();
		tt.start();
		for (int i = 0; i <100; i++) {	
			System.out.println(i+"22222222");
		}
	}

	@Override
	public void run() {
		for (int i = 0; i <100; i++) {			 
			System.out.println(i+"aaaaaaaaaa");
		}

	}

}
```
输出：（i+"22222222"和i+"aaaaaaaaaa"交替输出）
-------------------------------------------------------------------------

```java
public class TestThread2 {
	public static void main(String[] args) {
		Thread2 t2=new Thread2();
		Thread t=new Thread(t2);
		t.start();
		for (int i = 0; i < 100; i++) {
			System.out.println("-------------------");
		}
	}
}

class Thread2 implements Runnable {//多线程接口的实现，推荐使用这种方法。

	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println("子" + i);
		}
	}
}
```
输出：（"-------------------"和"子" + i交替输出）
-------------------------------------------------------------------------

```java
public class TestThread3 {
	public static void main(String[] args) throws InterruptedException {
		Thread3 t3 = new Thread3("the3name");//为线程命名
		t3.setName("the3newname");//为线程重命名
		t3.start();
//		t3.join();	
		for (int i = 0; i < 10; i++) {
			Thread.sleep(500);
			System.out.println(t3.getName()+i);
			if (i==4) {
				t3.join();		//join合并进来，不按照线程进行，而按照顺序。		
			}
		}
	}
}

class Thread3 extends Thread {
	public Thread3(String s) {
		super(s);
	}

	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
 				e.printStackTrace();
			}
			System.out.println("子。"+i);
		}
	}
}
```
输出：
子。0
the3newname0
子。1
the3newname1
the3newname2
子。2
子。3
the3newname3
the3newname4
子。4
子。5
子。6
子。7
子。8
子。9
the3newname5
the3newname6
the3newname7
the3newname8
the3newname9
-------------------------------------------------------------------------

```java
public class TestThread4 {
	public static void main(String[] args) {
		Thread4 t41 = new Thread4("W1----");
		Thread4 t42=new Thread4("M2++");
		t41.start();
		t42.start();
		for (int i = 0; i < 100; i++) {
			System.out.println("。。。main"+i);
		}
	}
}

class Thread4 extends Thread {
	public Thread4(String s) {
		super (s);
	}
	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println(this.getName()+i);
			if(i%5==0){
				Thread.yield();//当I为5的整倍数时候，线程被迫中止。
			}
		}		
	}
}
```
-------------------------------------------------------------------------

```java
import java.util.Timer;
import java.util.TimerTask;

class Task1 extends TimerTask {
	@Override
	public void run() {
		System.out.println("1");
	}
}
class Task2 extends TimerTask {
	@Override
	public void run() {
		System.out.println("2");
	}
}
class Task3 extends TimerTask {
	@Override
	public void run() {
		System.out.println("3");
	}
}

public class TestTimeTask {//定时器
	public static void main(String[] args) {
		Timer timer = new Timer();                                     
		timer.schedule(new Task2(),1000, 1000);   //构造函数有多种写法表达的意义不同
	}
}
```
 -------------------------------------------------------------------------


```java

//死锁
class TestDeadLock implements Runnable {
	public int flag = 1;
	static Object o1 = new Object(), o2 = new Object();
	public void run() {
		System.out.println("flag=" + flag);
		if(flag == 1) {
			synchronized(o1) {
				try {
					Thread.sleep(500);
				} catch (Exception e) {
					e.printStackTrace();
				}
				synchronized(o2) {
					System.out.println("1");
				}
			}
		}
		if(flag == 0) {
			synchronized(o2) {
				try {
					Thread.sleep(500);
				} catch (Exception e) {
					e.printStackTrace();
				}
				synchronized(o1) {
					System.out.println("0");
				}
			}
		}
	}	

	public static void main(String[] args) {
		TestDeadLock td1 = new TestDeadLock();
		TestDeadLock td2 = new TestDeadLock();
		td1.flag = 1;
		td2.flag = 0;
		Thread t1 = new Thread(td1);
		Thread t2 = new Thread(td2);
		t1.start();
		t2.start();

	}
}
```