Several ways to create a Java interview questions of threads and contrast?

During the interview process, several ways to create threads are frequently asked questions. So you really know it? In this, it was carried out finishing, for your reference is.

Compare of three ways:

a)

Use inheritance to create multi-threaded Thread class is a way advantage:

Write simple, if you need access to the current thread, you do not need to use Thread.currentThread () method, the direct use this to get the current thread.

disadvantage is:

Thread class Thread class has been inherited, so can not inherit other parent.

b)

Adopted to achieve Runnable, Callable interface means when Transcend multi-threaded, advantage is:

Thread class implements Runnable interface or just Callable interface, you can also inherit from other classes.

In this way, multiple threads may share the same target object, is the same for a plurality of threads to deal with a case where the same resources, which can be separated to form a clear model of the CPU, code and data, to better reflect the object-oriented thinking.

disadvantage is:

Programming is slightly more complicated, if you want to access the current thread, you must use Thread.currentThread () method.
 

There are three ways to create threads:

(1) inheritance Thread class Thread class is created.

    ① subclass definition Thread class, the class and overwrites the run () method. The method body run () method represents the thread to complete the task. Accordingly run () method is called executable.

    ② create an instance of a subclass of Thread, thread object is created.

    ③ calling thread object's start () method to start the thread.

public class ThreadTest extends Thread{
	int i = 0 ;
	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);
			//Thread.currentThread()方法返回当前正在执行的线程对象
			//getName()方法返回调用该方法的线程的名字
			if(i==20) {
				new ThreadTest().start();
				new ThreadTest().start();
			}
		}
	}
}

 

(2) achieved by the thread class Runnable interface.

    ① Runnable interface implementation class is defined, and overwrites the run () method of the interface. The method body run () method is also executable this thread.

    ② create an instance of the implementation class, and based on this example to create a Thread object, which is the real Thread object thread object.

    ③ calling thread object's start () method to start.

public class RunnableThreadTest implements Runnable{
	int i = 0 ;
	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) {
				RunnableThreadTest rtt = new RunnableThreadTest();
				new Thread(rtt,"new 线程1").start();
				new Thread(rtt,"new 线程2").start();
			}
		}
	}
}

(3) create a thread through Callable and Future.

   ① Creating Callable interface implementation class, and override the call () method. The call () method as a thread of execution, and returns a value

   ② Callable create an instance of the implementation class using FutureTask packaging Callable object, which object encapsulates the FutureTask Callable object Call return value () method.

   ③ use FutureTask Thread object as a target object to create and start a new thread.

   ④ call FutureTask object get () method to obtain the return value after the end of the sub-thread execution.

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
 
public class CallableThreadTest implements Callable<Integer>
{
 
	public static void main(String[] args)
	{
		CallableThreadTest ctt = new CallableThreadTest();
		FutureTask<Integer> ft = new FutureTask<>(ctt);
		for(int i = 0;i < 100;i++)
		{
			System.out.println(Thread.currentThread().getName()+" 的循环变量i的值"+i);
			if(i==20)
			{
				new Thread(ft,"有返回值的线程").start();
			}
		}
		try
		{
			System.out.println("子线程的返回值:"+ft.get());
		} catch (InterruptedException e)
		{
			e.printStackTrace();
		} catch (ExecutionException e)
		{
			e.printStackTrace();
		}
	}
	@Override
	public Integer call() throws Exception
	{
		int i = 0;
		for(;i<100;i++)
		{
			System.out.println(Thread.currentThread().getName()+" "+i);
		}
		return i;
	}
}

This article because I want to interview short-term study, so the reference to https://blog.csdn.net/longshengguoji/article/details/41126119

Thank the author of this article, thanks!

At the same time we hope that the advice we grow together, thank you!

Guess you like

Origin blog.csdn.net/qq_41026809/article/details/90485905