class mythread extends Thread{
private String title;
public mythread(String title) {
super();
this.title = title;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
for(int i=0;i<10;i++){
System.out.println(this.title+".x :"+i);
}
}
}jvm
//這樣實現多線好點,由於能夠不受單繼承的侷限
class mythread implements Runnable{
private String title;
public mythread(String title) {
super();
this.title = title;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<10;i++){
System.out.println(this.title+".x :"+i);
}
}
}ide
//爲啥不調用run而調用start
// private native void start0();
//它內部調用了本地的系統調用方法start0,從底層jvm進行對資源的
//協調,start最後也是要call run();this
//runnable沒返回值,callable有返回值
class Mythread implements Callable<String>{
private String title;
public Mythread(String title) {
this.title=title;
}
@Override
public String call() throws Exception {
for(int x=0;x<10;x++){
System.out.println(this.title+".x "+x);
}
return "ok";
}
}spa
public static void main(String[] args) throws Exception {
FutureTask<String> task=new FutureTask<>(new Mythread("A"));
new Thread(task).start();
System.out.println(task.get());
}.net