java线程使用示例——最简单的线程
线程使用示例一:
public class ThreadTest {
public static void main(String[] args) {
//线程使用示例一:
new Thread() {
public void run() {
while (true) {
try {
System.out.println("线程输出");
//休眠两秒
Thread.sleep(2 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start();
}
}
线程使用示例二: //线程使用示例二:
Thread t1 = new Thread(new Runnable(){
public void run(){
System.out.println("Mythread 线程t1");
while(true){
Singleton3 single3 = Singleton3.getInstance(null);
System.out.println("线程t1 >> " + single3.toString());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
t1.start();

本文提供了两个Java线程创建的实例,通过匿名内部类的方式实现Runnable接口来启动线程,示例展示了如何让线程执行特定任务并进行休眠操作。

被折叠的 条评论
为什么被折叠?



