java 实现线程的三种方式 Thread 、Runnable、 Callable Future

继承Thread类,重写run方法

例如:

/**
 * 实现线程方式一 继承Thread类
 * 
 * @author wbw
 * 
 */
public class MyThread1 extends Thread {

	@Override
	public void run() {

		try {
			for (int i = 0; i < 10; i++) {
				System.out.println("Hello" + i);
				Thread.sleep(10000);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
启动线程
package com.my.thread;

public class Test {
public static void main(String[] args) {
  
	new MyThread1().start();
}
}
实现Runnable接口

package com.my.thread;
/**
 * 实现方式二: 实现抽象接口Runnable,在run方法中编写业务逻辑代码
 * @author wbw
 *
 */
public class MyThread2  implements Runnable{

	@Override
	public void run() {
		try {
			for (int i = 0; i < 10; i++) {
				System.out.println("Hello" + i);
				Thread.sleep(10000);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	}

在上面的两种实现方式,可以看出都能不能有返回值和不能抛出异常

在JDK1.5提供的新特性 Callable 和Fucture 可以允许线程有返回值

Callable的接口

public interface Callable<V> { 

      V   call()   throws Exception; 

} 
Callable和Runnable的区别:

1. Callable定义的方法是call,而Runnable定义的方法是run。

2.  Callable的call方法可以有返回值,而Runnable的run方法不能有返回值。

3.  Callable的call方法可抛出异常,而Runnable的run方法不能抛出异常

4. Callable的启动可以通过创建ExecutorService对象调用sumbit()方法执行,也可以通过Thread的start()方法,而Runnable通过Thread的start()方法 

例子:
<strong>package com.my.thread;

import java.util.concurrent.Callable;
/**
 * 实现方式三:实现Callable接口
 * @author wbw
 *
 */
public class MyThread3 implements Callable<String> {

	@Override
	public String call() throws Exception {
		
		return "hello world";
	}

}</strong>
启动:

<strong>package com.my.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {
public static void main(String[] args) {
  
	//Callable的启动方式
	MyThread3  task = new MyThread3();
	ExecutorService es =  Executors.newCachedThreadPool();
	es.submit(task);
}
}</strong>
Future用来保存Callable异步运算的结果

FutureTask封装Future的实体类

Future的接口如下:

<strong>public interface Future<V> {   
    boolean cancel(boolean mayInterruptIfRunning);   
    boolean isCancelled();   
    boolean isDone();   
    V get() throws InterruptedException, ExecutionException;   
    V get(long timeout, TimeUnit unit)   
        throws InterruptedException, ExecutionException, TimeoutException;   
}   </strong>
Callable 与Future
<strong>package com.my.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
 * 测试Callable
 * @author wbw
 *
 */
public class Test {
public static void main(String[] args) {
  
	 try {
			//创建callable任务
			Callable<String>  task = new MyThread3();
		   // 创建一个执行任务的服务
			ExecutorService es =  Executors.newCachedThreadPool();
		    // 提交并执行任务,任务启动时返回了一个Future对象,
			 Future<String> future =  es.submit(task);
			 // 获得第一个任务的结果,如果调用get方法,当前线程会等待任务执行完毕后才往下执行,形成阻塞的状态
		    System.out.println("Callable线程的返回值:"+future.get());
		    //停止任务执行服务
		    es.shutdownNow();
	} catch (InterruptedException | ExecutionException e) {
		e.printStackTrace();
	}
}
}</strong>
Callable 与FutureTask
<strong>	    try {
	    	Callable<String>  task2 = new MyThread3();
			//创建FutureTask对象
			FutureTask<String> ft = new FutureTask<String>(task2);
		    Thread thread = new Thread(ft);
		    thread.start();//启动
			Thread.sleep(100);
			if(!ft.isDone()){//判断当前线程的执行状态
				System.out.println(ft.get());
			}
			if(!ft.isCancelled()){//判断当前线程是否已被取消
				ft.cancel(true);
				System.out.println("Cancelled");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}</strong>
三种实现线程的方式,根据实际情况选用


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值