Joke Collection Website - Public benefit messages - What are the ways to realize multithreading in Java and how to use it?

What are the ways to realize multithreading in Java and how to use it?

Creating and starting Java multiThreading

There are three basic forms of thread creation in Java

1. Inherit the thread class and rewrite the run () method of this class.

copy the code

1 class my thread extends thread {

2

3 private int I = ;

4?

5 @Override

6 public void run() {

7 for (i = ; i < 1; i++) {

8 System.out.println(Thread.currentThread().getName() + " " + i);

9 }

1 }

11 }

copy code

copy code

1 public class ThreadTest {

2?

3 public static void main(String[] args) {

4 for (int i = ; i < 1; i++) {

5 System.out.println(Thread.currentThread().getName() + " " + i);

6 if (i == 3) {

7 Thread myThread1 = new MyThread(); //Create a new thread? myThread1 ? This thread enters the new state

8 Thread myThread2 = new MyThread (); //create a new thread myThread2. This thread enters the new state

9 myThread1.start (); //call the start () method to make the thread enter the ready state

1 myThread2.start (); //calling the start () method makes the Thread enter the ready state

11 }

12 }

13 }

14 }

Copy the code

As shown above, inherit the thread class and define a new thread class MyThread by rewriting the run () method, in which the method body of the run () method represents the thread. When this thread class object is created, a new thread is created and enters the new thread state. By calling the start () method referenced by the thread object, the thread will enter the ready state, at which time the thread may not be executed immediately, depending on the CPU scheduling opportunity.

2. implement the Runnable interface and rewrite the run () method of the interface, which is also a Thread executor, and create an instance of the Runnable implementation class, and use this instance as the target of the Thread class to create a Thread object, which is the real thread object.

copy code

1class my runnable implementations runnable {

2private int I = ;

3?

4 @Override

5 public void run() {

6 for (i = ; i < 1; i++) {

7 System.out.println(Thread.currentThread().getName() + " " + i);

8 }

9 }

1 }

copy code

copy code

1 public class ThreadTest {

2?

3 public static void main(String[] args) {

4 for (int i = ; i < 1; i++) {

5 System.out.println(Thread.currentThread().getName() + " " + i);

6 if (i == 3) {

7 Runnable myRunnable = new MyRunnable(); //create an object of the Runnable implementation class

8threadthread1 = newthread (my runnable); //create a new thread with myRunnable as Thread target

9threadthread2 = newthread (myrunnable);

1 thread1.start(); //Call the start () method to make the thread enter the ready state

11 thread2.start ();

12 }

13 }

14 }

15 }

Copying code

I believe everyone is familiar with the above two ways to create a new Thread, so what is the relationship between Thread and Runnable? Let's take a look at the following example first.

copy the code

1 public class ThreadTest {

2?

3 public static void main(String[] args) {

4 for (int i = ; i < 1; i++) {

5 System.out.println(Thread.currentThread().getName() + " " + i);

6 if (i == 3) {

7 Runnable myRunnable = new MyRunnable();

8 Thread thread = new MyThread(myRunnable);

9 thread.start();

1 }

11 }

12 }

13 }

14?

15 class MyRunnable implements Runnable {

16 private int i = ;

17?

18 @Override

19 public void run() {

2 System.out.println("in MyRunnable run");

21 for (i = ; i < 1; i++) {

22 System.out.println(Thread.currentThread().getName() + " " + i);

23 }

24 }

25 }

26?

27 class MyThread extends Thread {

28?

29 private int i = ;

3

31 public MyThread(Runnable runnable){

32 super(runnable);

33 }

34?

35 @Override

36 public void run() {

37 System.out.println("in MyThread run");

38 for (i = ; i < 1; i++) {

39 System.out.println(Thread.currentThread().getName() + " " + i);

4 }

41 }

42 }

Copying code

is similar to creating threads by implementing the Runnable interface, with the difference that

1thread thread = new mythread (my runnable);

so can you create a new thread successfully in this way? The answer is yes. As for the thread executor at this time, is it the run () method in MyRunnable interface or the run () method in MyThread class? Through the output, we know that the thread executor is the run () method in the MyThread class. In fact, the reason is very simple, because the Thread class itself also implements the Runnable interface, and the run () method was first defined in the Runnable interface.

1 public interface Runnable {

2

3 public abstract void run();

4

5 }

Let's take a look at the implementation of the run () method in the Runnable interface in the Thread class:

Copy the code

@ override

public void run () {

if (target! = null) {

target.run();

}

}

Copy the code

That is to say, when executing the run () method in the Thread class, it will first determine whether the target exists, and if it exists, execute the run () method in the target, that is, it implements the Runnable interface and rewrites the run () method in the class. However, due to the polymorphism, the run () method in the Thread class is not executed at all, but the run () method in the MyThread class is directly executed first.

3. create a thread using Callable and Future interfaces. Specifically, the implementation class of Callable interface is created, and the clall () method is implemented. And use FutureTask class to wrap the object of Callable implementation class, and use this FutureTask object as the target of Thread object to create thread.

It seems a bit complicated. Just look at an example directly.

copy the code

1 public class ThreadTest {

2?

3 public static void main(String[] args) {

4?

5 Callable< Integer> myCallable = new MyCallable(); //Create MyCallable object

6FutureTask <; Integer> ft = new FutureTask< Integer> (myCallable); //wrap MyCallable object with FutureTask

7?

8 for (int i = ; i < 1; i++) {

9 System.out.println(Thread.currentThread().getName() + " " + i);

1 if (i == 3) {

11 Thread thread = new Thread(ft); ? //FutureTask object as the target of Thread object creates a new thread

12 thread.start (); ? //The thread enters the ready state

13 }

14 }

15?

16 System.out.println ("main thread for loop execution finished ...");

17

18 try {

19 int sum =