@v-cong
2016-03-22T15:33:40.000000Z
字数 1107
阅读 1119
Java多线型
import java.util.Date;public class StartThread { public void startY(){ ThreadY ty = new ThreadY(); ty.startThreadY(); try{ Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } ty.stopThreadY(); } public void startX() { Runnable runnX = new ThreadX(); Thread threadX = new Thread(runnX); threadX.start(); } public static void main(String[] args){ StartThread test = new StartThread(); test.startY(); test.startX(); }}class ThreadY extends Thread{ private boolean isRunState = false; public void start(){ this.isRunState = true; super.start(); } public void run(){ int i = 0; try { while (isRunState){ this.setName("Thread-"+ i ++); System.out.println("线程Y:" + this.getName() + "正在运行"); Thread.sleep(100); } }catch (Exception e ){ } System.out.println(this.getName()+ "运行结束..."); } public void setRunning(boolean isRunState){ this.isRunState = isRunState; } public void startThreadY(){ System.out.println("启动线程Y..."); this.start(); } public void stopThreadY(){ System.out.println("结束线程Y。。。"); this.setRunning(false); }}class ThreadX implements Runnable { private Date runnDate; public void run(){ System.out.println("线程X已启动。。。"); this.runDate = new Date(); System.out.println("启动时间" + runDate.toLocaleString()); }}