java 多线程 实现 无返回值 有返回值 Runnable Thread Callable<T> Future<String> FutureTask<String> 线程池获取

436 阅读2分钟

目录

介绍

实现示例

直接调用

实现Runnable接口

继承Thread 实现

获取返回值

实现join 获取返回值

实现  Callable  和 FutureTask 获取返回数据

线程池 配合 Callable 实现返回



介绍

这里篇文章介绍 java 线程实现,有返回值和无返回值的实现方法,以及 获取返回值的方法

实现 Runnable 继承 Thread 实现 Callable<t>  获取返回值 future  futureTask  或线程池 ExecutorService  .submit(Callable<t>实现类);

上一篇:java 线程锁 可重入锁 可中断锁 公平锁 非公平锁ReentrantLock synchronized,条件Condition,读写锁 ReentrantReadWriteLock写写互斥读读共享

yushen.blog.csdn.net/article/det…

 

实现示例

直接调用

public class test1 {
	public static void main(String[] args) {
		for (int i = 0; i < 3; i++) {
			final int nowi = i;
			new Thread(new Runnable() {
				@Override
				public void run() {
					for (int j = 0; j < 3; j++) {
						System.out.println(nowi+"任务运行中"+j);
					}
				}
			}).start();
		}
	}
}

 

实现Runnable接口

public class test1 {
	public static void main(String[] args) {
		for (int i = 0; i < 3; i++) {
			new Thread(new t1(i+"")).start();
		}
	}
}
class t1 implements Runnable{
	private String taskName = "";
	public t1(String taskName){
		this.taskName=taskName;
	}
	@Override
	public void run() {
		for (int i = 0; i <3; i++) {
			System.out.println(taskName+"任务正在执行:"+i);
		}
	}
}

 

继承Thread 实现

public class test1 {
	public static void main(String[] args) {
		for (int i = 0; i < 3; i++) {
//			new Thread(new t1(i+"")).start();
			new t2(i+"").start();
		}
	}
}
class t2 extends Thread{
	private String taskName = "";
	public t2(String taskName){
		this.taskName=taskName;
	}
	@Override
	public void run() {
		for (int i = 0; i <3; i++) {
			System.out.println(taskName+"任务正在执行:"+i);
		}
	}
}

 

 

获取返回值

 

实现join 获取返回值

public class test1 {
	public static void main(String[] args) {

		t2 t = new t2(9);
		t.start();
		try {
			t.join();
			System.out.println("执行完后,num="+t.getNum());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		
	}
}
class t2 extends Thread{
	private int num = 0;
	public t2(int num){
		this.num=num;
	}
	public int getNum(){
		return this.num;
	}
	@Override
	public void run() {
		for (int i = 0; i <3; i++) {
			System.out.println("任务正在执行++:第"+i+"次");
			this.num++;
		}
	}
}

 

实现  Callable<t>  和 FutureTask 获取返回数据


public class test1 {
	public static void main(String[] args) {
		FutureTask<String> task = new FutureTask<String>(new t3(9));
		new Thread(task).start();

		if (!task.isDone()) {
			System.out.println("任务尚未完成,请稍候!");
		}

		try {
			System.out.println("任务返回: " + task.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}
}

class t3 implements Callable<String> {
	private int num = 0;

	public t3(int num) {
		this.num = num;
	}

	@Override
	public String call() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return num + "5578";
	}
}

方法

  • isDone:利用state变量判断call方法有没有被执行
  • get:如果call方法已经执行完就返回call方法的返回值,如果call方法没有执行完就一直阻塞

 

线程池 配合 Callable<T> 实现返回


public class test1 {
	public static void main(String[] args) {
		//创建线程池
        ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
        //通过线程池管理线程MyCallable
        Future<String> future = newCachedThreadPool.submit(new t3(98));

        //如果提交的任务未完成
        if (!future.isDone()) {
            System.out.println("运行中!");
        }

        try {
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } finally {
            //关闭线程池
            newCachedThreadPool.shutdown();
        } 
	}
}

class t3 implements Callable<String> {
	private int num = 0;

	public t3(int num) {
		this.num = num;
	}

	@Override
	public String call() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return num + "5578";
	}
}

 

 

 

上一篇:java 线程锁 可重入锁 可中断锁 公平锁 非公平锁ReentrantLock synchronized,条件Condition,读写锁 ReentrantReadWriteLock写写互斥读读共享

yushen.blog.csdn.net/article/det…

 

 

 

ok

 

 

 

 

 

持续更新