日期:2014-05-20 浏览次数:20775 次
package com.zjfly.test;
import java.util.concurrent.TimeUnit;
public class ThreadTest {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
MyThreadClsss myThreadClass1 = new MyThreadClsss();
MyThreadClsss myThreadClass2 = new MyThreadClsss();
MyThreadClsss myThreadClass3 = new MyThreadClsss();
myThreadClass1.setTname("Thread1");
myThreadClass2.setTname("Thread2");
myThreadClass3.setTname("Thread3");
Thread thread1 = new Thread(myThreadClass1);
Thread thread2 = new Thread(myThreadClass2);
Thread thread3 = new Thread(myThreadClass3);
thread1.run();
thread2.run();
thread3.run();
TimeUnit.SECONDS.sleep(4);
System.out.println("********************************");
MyThreadClsss.stopRequested=false;
System.out.println("stopRequested =" +MyThreadClsss.stopRequested);
}
}
class MyThreadClsss implements Runnable {
public static boolean stopRequested = true;
private String tname;
@Override
public void run() {
int i = 0;
while(stopRequested){
try {
i++;
System.out.println("myanme ="+this.getTname()+" i="+i);
TimeUnit.SECONDS.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public String getTname() {
return tname;
}
public void setTname(String name) {
this.tname = name;
}
}