构造方法:
常用方法:
创建线程的方式:
1.继承Thread类
public class MyThread extends Thread {
public MyThread() {
}
public MyThread(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
System.out.println(getName()+":"+i);
}
}
}
public class MyThreadDome {
public static void main(String[] args) {
MyThread mt = new MyThread();
mt.start();
mt.setPriority(10);
for (int i = 0; i <10 ; i++) {
System.out.println(Thread.currentThread().getName()+"-->"+i);
}
}
}
2.实现Runnable接口
public class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i <5 ; i++) {
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
public class MyRunnableTest {
public static void main(String[] args) {
MyRunnable mr = new MyRunnable();
Thread th = new Thread(mr);
th.start();
for (int i = 0; i <5 ; i++) {
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
原文:https://www.cnblogs.com/lifengSkt/p/13267889.html