线程的创建
2021-06-08 12:02
标签:lock public rri rate main exception integer cep int 线程创建三个方法: 1.继承thread类 2.实现runnable接口 3.实现callable接口 实例: //线程的创建方法 public static void main(String[] args) { //1.继承thread类 //2.实现runnable接口 @Override //3.实现callable接口 @Override
public class TestNew {
new Mythread1().start();
Mythread2 mythread2=new Mythread2();
new Thread(mythread2).start();
FutureTask
new Thread(futureTask).start();
try {
Integer integer=futureTask.get();
System.out.println(integer);
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Mythread1 extends Thread{
@Override
public void run() {
System.out.println("Mythread1");
}
}
class Mythread2 implements Runnable{
public void run() {
System.out.println("Mythread2");
}
}
class Mythread3 implements Callable
public Integer call() throws Exception {
System.out.println("Mythread3");
return 100;
}
}