当前位置:网站首页>Quickly understand the three ways of thread implementation

Quickly understand the three ways of thread implementation

2022-04-23 14:15:00 pureluckyfish

One 、 Three ways to implement threads :

         Mode one 、 Inherit Thread class , make carbon copies run() Method

class AThread extends Thread {
	@Override
	public void run() {
		System.out.println("AThread:" + Thread.currentThread().getName());
		super.run();
	}
}

         Mode two 、 Realization Runnable Interface , make carbon copies run() Method

class BThread implements Runnable {
	@Override
	public void run() {
		System.out.println("BThread:" + Thread.currentThread().getName());

	}
}

         Mode three 、 Realization Callable Interface , make carbon copies call() Method

class CThread implements Callable<Object> {
	@Override
	public Object call() throws Exception {
		System.out.println("CThread:" + Thread.currentThread().getName());
		return Thread.currentThread().getName();
	}
}

Two 、 Threads execute

package ThreadStudy;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class T1 {
	public static void main(String[] args) throws Exception {
		//  The first way is to realize 
		AThread t1 = new AThread();
		t1.start();

		//  Mode 2 implementation ( One )
		Thread t2 = new Thread(new BThread());
		t2.start();

		//  Mode 2 implementation ( Two ): Anonymous inner class 
		Thread t3 = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out.println("main:" + Thread.currentThread().getName());

			}
		});
		t3.start();

		//  Mode 3 Implementation ( One ): Execution in thread pool Callable The thread gets the return value 
		ExecutorService exec = Executors.newSingleThreadExecutor();
		CThread ct1 = new CThread();
		Future f1 = exec.submit(ct1);
		System.out.println(" Mode 3 Implementation ( One ):" + f1.get());
		
		//  Mode 3 Implementation ( Two ):FutureTask Class to perform Callable The thread gets the return value 
		CThread ct2 = new CThread();
		FutureTask<Object> f2 = new FutureTask<Object>(ct2);
		exec.submit(f2);
		System.out.println(" Mode 3 Implementation ( Two ):" + f2.get());
		exec.shutdown();
	}
}

// Mode one 、 Inherit Thread class , make carbon copies run() Method 
class AThread extends Thread {
	@Override
	public void run() {
		System.out.println("AThread:" + Thread.currentThread().getName());
		super.run();
	}
}

// Mode two 、 Realization Runnable Interface , make carbon copies run() Method 
class BThread implements Runnable {

	@Override
	public void run() {
		System.out.println("BThread:" + Thread.currentThread().getName());

	}

}

// Mode three 、 Realization Callable Interface , make carbon copies call() Method 
class CThread implements Callable<Object> {

	@Override
	public Object call() throws Exception {
		System.out.println("CThread:" + Thread.currentThread().getName());
		return Thread.currentThread().getName();
	}

}

3、 ... and 、Runnable Specify the return value after successful execution

You can specify any type of return result <T> Future<T> submit(Runnable task, T result);

         Code implementation

package ThreadStudy;

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

public class TestTmp {
	public static void main(String[] args) throws Exception {

		ExecutorService exec = Executors.newSingleThreadExecutor();
		Future future = exec.submit(new Runnable() {

			@Override
			public void run() {
				System.out.println("Runnable Start execution ");
			}
		}, "success");
		System.out.println(" obtain Runnable The returned result after execution :" + future.get());

	}
}

Four 、Callable And Runnable difference :

Runnable Callable
1、 Different replication methods run() call()
2、 With or without return value no yes
3、 Different execution methods

It can execute in the process pool

Can be in again Thread Class to perform

Can only be executed in the thread pool


 

版权声明
本文为[pureluckyfish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231406486054.html