[JAVA100例]061、实现Runnable[Java编程]
本文“[JAVA100例]061、实现Runnable[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
/**
* <p>Title: 实现Runnable接口,得到线程.</p>
* <p>Description: 通过实现Runnable接口来得到自己的线程(t2).</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: twothread.java</p>
* @version 1.0
*/
public class twothread implements Runnable {
/**
*<br>办法阐明:构造器.实际线程,并启动这个线程.
*<br>输入参数:
*<br>返回范例:
*/
twothread() {
//获得当前的线程
Thread t1 =Thread.currentThread();
t1.setName("The first main thread");
System.out.println("The running thread:" + t1);
//通过将本类(Runnable接口)和名称构造一个线程
Thread t2 = new Thread(this,"the second thread");
System.out.println("creat another thread");
//启动线程
t2.start();
try {
System.out.println("first thread will sleep");
Thread.sleep(3000);
}catch (InterruptedException e) {
System.out.println("first thread has wrong");
}
System.out.println("first thread exit");
}
/**
*<br>办法阐明:线程主体.实现run办法.
*<br>输入参数:
*<br>返回范例:
*/
public void run() {
try {
for (int i=0;i<5;i++) {
System.out.println("Sleep time for thread 2:"+i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("thread has wrong");
}
System.out.println("second thread exit");
}
/**
*<br>办法阐明:主办法
*<br>输入参数:
*<br>返回范例:
*/
public static void main(String args[]) {
new twothread();
}
}
以上是“[JAVA100例]061、实现Runnable[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |