多线程Callable接口创建多线程。

package www.homework.java;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class MyThreadC implements Callable<String>{
    private int ticket=10;
    @Override
    public String call() throws Exception {
        while (this.ticket>0) {
            System.out.println("剩余票数:"+ticket--);
            
        }
        return "票卖完了";
    }
    
}

public class Duoxiancheng_Callable {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        MyThreadC myThreadC=new MyThreadC();
        FutureTask<String>futureTask=new FutureTask<>(myThreadC);
        Thread thread=new Thread(futureTask);
        Thread thread1=new Thread(futureTask);
        Thread thread2=new Thread(futureTask);
        
        thread.start();
        thread1.start();
        thread2.start();
        System.out.println(futureTask.get());
    }
    
    
}
 

猜你喜欢

转载自blog.csdn.net/sd116460/article/details/81237040