## java语言对线程的支持
java.lang
Class Thread
interface Runnable
线程实际执行的共同代码public void run()
~~~java
//继承Thread创建线程和实现Runnable接口创建线程
public class Actor extends Thread {
public void run() {
//getName()获取线程名称
System.out.println(getName()+"是一个演员!");
int count = 0;
boolean keepRunning = true;
while (keepRunning) {
System.out.println(getName()+"登台演出:"+(++count));
if(count == 100){
keepRunning = false;
}
//当演出count能被10整除时,让线程睡眠
if(count%10 == 0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println(getName()+"的演出结束了");
}
public static void main(String[] args) {
Thread actor = new Actor();
actor.setName("Mr.Thread");
actor.start();
Thread actressThread = new Thread(new Actress(),"Ms.Runnable");
actressThread.start();
}
}
class Actress implements Runnable{
public void run() {
//getName()获取d当前线程名称
System.out.println(Thread.currentThread().getName()+"是一个演员!");
int count = 0;
boolean keepRunning = true;
while (keepRunning) {
System.out.println(Thread.currentThread().getName()+"登台演出:"+(++count));
if(count == 100){
keepRunning = false;
}
//当演出count能被10整除时,让线程睡眠
if(count%10 == 0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println(Thread.currentThread().getName()+"的演出结束了");
}
}
~~~
### 线程的创建和启动实例
隋唐演义三个对象:
ArmyRunnable实现Runnable接口为两支军队的线程
Stage为主线程,其余线程在Stage中创建
KeyPersonThread线程,start是用join方法使其他线程等待KeyPersonThread线程运行完
#### 军队-ArmyRunnable:
~~~java
public class ArmyRunnable implements Runnable {
//volatitle保证了线程可以正确的读取其他线程写入的值
//可见性 ref JMM ,happens-before原则
volatile boolean keepRunning = true;
//军队线程模拟作战双方的行为
public void run() {
//ctrl+1,新建布尔值
while(keepRunning){
//发动5连击
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+"进攻对方["+i+"]");
//让出了处理器时间,下次谁进攻不一定
Thread.yield();
}
}
System.out.println(Thread.currentThread().getName()+"结束了战斗!");
}
}
~~~
#### 英雄人物-KeyPersonThread;
~~~java
public class KeyPersonThread extends Thread {
public void run() {
System.out.println(Thread.currentThread().getName()+"开始了战斗!");
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+"左突右杀,攻击隋军..");
}
System.out.println(Thread.currentThread().getName()+"结束了战斗!");
}
}
~~~
#### 舞台-Stage
~~~java
/**
* 舞台
*/
public class Stage extends Thread {
public static void main(String[] args) {
new Stage().start();
}
public void run() {
System.out.println("开始");
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("话说.....");
ArmyRunnable armyTaskOfSuiDynasty = new ArmyRunnable();
ArmyRunnable armyTaskOfRevolt = new ArmyRunnable();
// 使用Runnable接口创建线程
Thread armyOfSuiDynasty = new Thread(armyTaskOfSuiDynasty, "隋军");
Thread armyOfRevolt = new Thread(armyTaskOfRevolt, "农民起义军");
// 启动线程
armyOfSuiDynasty.start();
armyOfRevolt.start();
// 舞台线程休眠
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
armyTaskOfSuiDynasty.keepRunning = false;
armyTaskOfRevolt.keepRunning = false;
try {
armyOfRevolt.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("半路杀出咬");
Thread mrCheng = new KeyPersonThread();
mrCheng.setName("程咬金");
System.out.println("程咬金理想结束战争");
armyTaskOfSuiDynasty.keepRunning = false;
armyTaskOfRevolt.keepRunning = false;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
/**
* 关键人物登台
*/
mrCheng.start();
//所有线程等待程先生完成任务
try {
mrCheng.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("结束!");
}
}
~~~
### 线程的常用方法
![](http://www.kancloud.cn/book/preview/image?book_id=15055&file=image/570ca35aad87f.png&ref=master)