Java实现线程的方法

有四种方法

1)继承Thread  

public class MyThread extends Thread {  
  public void run() {  
   System.out.println("MyThread.run()");  
  }  
} 
2) 实现 Runnable 接口
public class MyRunnable implements Runnable {  

    @Override
  public void run() {  
   System.out.println("MyThread.run()");  
  }  
}  

Thread runnable = new Thread(new MyRunnable());

3) Callable  FutureTask来实现线程

class ThreadDemo implements Callable<Integer> {
 
    @Override
    public Integer call() throws Exception {
        int sum = 0;
 
        for (int i = 0; i <= 100000; i++) {
            sum += i;
        }
 
        return sum;
    }
}
 
public class TestCallable {
 
    public static void main(String[] args) {
        ThreadDemo td = new ThreadDemo();
 
        //1.执行 Callable 方式,需要 FutureTask 实现类的支持,用于接收运算结果。
        FutureTask<Integer> result = new FutureTask<>(td);
 
        new Thread(result).start();
 
        //2.接收线程运算后的结果
        try {
            Integer sum = result.get();  //FutureTask 可用于 闭锁 类似于CountDownLatch的作用,在所有的线程没有执行完成之后这里是不会执行的
            System.out.println(sum);
            System.out.println("------------------------------------");
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
 
}

FutureTask在调用get时,方法会阻塞, 直到任务返回结果,后面的代码才会执行

4)使用线程池方法 : 用ExecutorService、Callable、Future实现有返回结果的线程

public class TestCallable<String> implements Callable<String> {

    String appid;

    public TestCallable(java.lang.String appid) {
        this.appid = (String) appid;
    }

    @Override
    public String call() {
    	 System.out.println("task" + this.appid+"开始");
        try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
        System.out.println("task" + this.appid+"睡了3s");
        
//        System.err.println("task" + this.appid + "睡了3s");
        return (String) ("task" + this.appid+ " return");
    }
public class TestCallable2<String> implements Callable<String>{
	
	
	public String test1;
	
	public String test2;
	
	
    public TestCallable2(String test1, String test2) {
       this.test1 = test1;
       this.test2 = test2;

    }

	@Override
	public String call() throws Exception {
		// TODO Auto-generated method stub
		return (String) (test1.toString() + test2.toString());
	}

}
public class Client { 
  
  public static void main(String[] args) throws Exception { 
    ExecutorService pool = Executors.newFixedThreadPool(4); 
  
       TestCallable t1 = new TestCallable("test1"); 
      TestCallable t2 = new TestCallable("test2"); 
  
    Future<String> f1 = pool.submit(t1); 
    Future<String> f2 = pool.submit(t2); 
      
    //Future调用get方法时,如果线程还没有执行完,程序阻塞在这里 
    TestCallable2 t3 = new TestCallable2(f1.get(), f2.get()); 
    Future<Integer> f5 = pool.submit(t3); 
      
    System.out.println(f3.get()); 
      
    pool.shutdown(); 
    }
}
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值