Several Ways to Create Threads in JAVA

Keywords: Programming Java

Java creates threads in four ways:

 

1. Inheritance of Thread class

2. Implementing Runnable Interface

3. ExecuteService, Callable<Class>, Future with Return Threads

4. Thread Pool Approach

 

First, inherit Thread class and create thread class

(1) Define a subclass of the Thread class and override the run method of that class. The body of the run method represents the task that the thread wants to accomplish. So the run() method is called an executor.

(2) Create an instance of Thread subclass, that is, create thread objects.

(3) Call the start() method of the thread object to start the thread.

package jichu_0;

public class ForWays_Thread extends Thread {
	int i = 0;

	// Rewrite the run method. The body of the run method is the field executor.
	public void run() {
		for (; i < 100; i++) {
			System.out.println(getName() + "  " + i);
		}
	}

	public static void main(String[] args) {
		for (int i = 0; i < 100; i++) {
			System.out.println(Thread.currentThread().getName() + "  : " + i);
			if (i == 20) {
				new ForWays_Thread().start();
				new ForWays_Thread().start();
			}
		}
	}

}

Thread.currentThread()Method returns the thread object currently being executed. GetName()Method returns the name of the thread calling the method.

2. Creating Thread Classes through Runnable Interface

(1) Define the implementation class of the runnable interface and override the run() method of the interface. The run() method body is also the thread executor of the thread.

(2) Create an instance of the Runnable implementation class and use it as the target of Thread to create the Thread object, which is the real thread object.

(3) Call the start() method of the thread object to start the thread.
 

package jichu_0;

public class ForWays_Runnable implements Runnable {
	private int i;

	public void run() {
		for (i = 0; i < 100; i++) {
			System.out.println(Thread.currentThread().getName() + " " + i);
		}
	}

	public static void main(String[] args) {
		for (int i = 0; i < 100; i++) {
			System.out.println(Thread.currentThread().getName() + " " + i);
			if (i == 20) {
				ForWays_Runnable rtt = new ForWays_Runnable();
				new Thread(rtt, "New thread 1").start();
				new Thread(rtt, "New thread 2").start();
			}
		}
	}
}

3. Creating threads through Callable and Future

(1) Create an implementation class of the Callable interface and implement the call() method, which acts as a thread executor with a return value.

(2) Create an instance of the Callable implementation class and wrap the Callable object with the FutureTask class, which encapsulates the return value of the Callable object's call() method.

(3) Use the FutureTask object as the target of the Thread object to create and start new threads.

(4) Call the get() method of the FutureTask object to get the return value of the subthread after execution
 

public class ForWays_Callable implements Callable<Integer>{
	@Override
	public Integer call() throws Exception{	
		int i = 0;
		for(;i<100;i++){		
			System.out.println(Thread.currentThread().getName()+" "+i);
		}
		return i;
	}
	
	public static void main(String[] args){
		ForWays_Callable ctt = new ForWays_Callable();
		FutureTask<Integer> ft = new FutureTask<>(ctt);
		
		for(int i = 0;i < 100;i++){		
			System.out.println(Thread.currentThread().getName()+" Cyclic variables i Value"+i);
			if(i==20){			
				new Thread(ft,"Threads with return values").start();
			}
		}
		
		try{		
			System.out.println("The return value of the subthread:"+ft.get());
		} catch (InterruptedException e){	
			e.printStackTrace();
		} catch (ExecutionException e)
		{
			e.printStackTrace();
		}

	}

}

 

The call() method can have a return value

The call() method can declare that an exception is thrown

Java 5 provides a Future interface to represent the return value of the call() method in the Callable interface, and provides an implementation class FutureTask for the Future interface, which implements both the Future interface and the Runnable interface.

Posted by koldstar on Sat, 05 Oct 2019 07:19:01 -0700