1.继承Thread类
public class ExtendsThreadWay extends Thread{
@Override
public void run() {//重写 run 方法
for (int i = 0; i < 10; i++) {
System.out.println("------多线程-------" + i);
}
}
public static void main(String[] args) {
ExtendsThreadWay et = new ExtendsThreadWay();
et.start();
for (int i = 0; i < 10; i++) {
System.out.println("主线程" + i);
}
}
}
2.实现runable 接口
public class AchieveRunnableWay implements Runnable{
@Override
public void run() {//实现接口
for (int i = 0; i < 10; i++) {
System.out.println("------多线程-------" + i);
}
}
public static void main(String[] args) {
Thread t = new Thread(new AchieveRunnableWay());
t.start();
for (int i = 0; i < 10; i++) {
System.out.println("主线程" + i);
}
}
}
3.实现callable接口的future对象
public class AchieveRunnableWay implements Runnable{
@Override
public void run() {//实现接口
for (int i = 0; i < 10; i++) {
System.out.println("------多线程-------" + i);
}
}
public static void main(String[] args) {
Thread t = new Thread(new AchieveRunnableWay());
t.start();
for (int i = 0; i < 10; i++) {
System.out.println("主线程" + i);
}
}
}