Tread thread = new Thread();
执行该线程可以调用该线程的start()方法:
thread.start();
public class MyThread extends Thread {
public void run(){
System.out.println("MyThread running");
}
} Thread thread = new Thread(){
public void run(){
System.out.println("Thread Running");
}
};
thread.start(); public class MyRunnable implements Runnable {
public void run(){
System.out.println("MyRunnable running");
}
} Thread thread = new Thread(new MyRunnable());
thread.start();
Runnable myRunnable = new Runnable(){
public void run(){
System.out.println("Runnable running");
}
}
Thread thread = new Thread(myRunnable);
thread.start(); Thread newThread = new Thread(MyRunnable());
newThread.run(); //should be start();
MyRunnable runnable = new MyRunnable();
Thread thread = new Thread(runnable, "New Thread");
thread.start();
System.out.println(thread.getName());
public class ThreadExample {
public static void main(String[] args){
System.out.println(Thread.currentThread().getName());
for(int i=0; i<10; i++){
new Thread("" + i){
public void run(){
System.out.println("Thread: " + getName() + "running");
}
}.start();
}
}
}| 欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |