This is commonly used because multithreading can also be achieved through the thread pool in the Java.util.concurrent package. We'll go into details about the thread pool, and now we'll get to the thread and runnable. The contents of this chapter include:
Introduction to Thread and runnable
The similarities and differences between thread and runnable
Multithreading examples of thread and runnable
Introduction to Thread and runnable
Runnable is an interface that contains only a run () method. It is defined as follows:
Public interface Runnable {
public abstract void run ();
}
runnable function, realize multithreading. We can define a class A to implement the Runnable interface, and then create a new thread by means of the new thread (new A ()).
Thread is a class. Thread itself implements the Runnable interface. It declares the following:
public class Thread implements Runnable {}
Thread's role in implementing multithreading.
The similarities and differences between thread and runnable
The same point of thread and Runnable: "Multithreading is the way to implement."
Different points of Thread and Runnable:
Thread is a class, and runnable is an interface; The thread itself is the class that implements the Runnable interface. We know that "a class can have only one parent class, but it can implement multiple interfaces," So runnable has better extensibility.
In addition, runnable can also be used for "resource sharing". That is, multiple threads are built on a Runnable object that shares the resources on the Runnable object.
In general, it is recommended to implement multithreading through "Runnable"!
Multithreading examples for thread and runnable
1. Thread's multithreaded example
The following example is a better understanding of thread and runnable, using an example on the Internet to compare persuasive examples.
1//Threadtest.java source
2 class MyThread extends thread{
3 private int ticket=10;
4 public void Run () {
5 for (int i=0;i<20;i++) {
6 if (this.ticket>0) {
7 System.out.println (This.getname () + "Sell ticket: Ticket" +this.ticket--);
8}
3 ·
10}
11};
12
public class ThreadTest {
public static void Main (string[] args) {
15//Start 3 thread t1,t2,t3; sell 10 tickets per thread!
MyThread t1=new MyThread ();
MyThread t2=new MyThread ();
MyThread t3=new MyThread ();
T1.start ();
T2.start ();
T3.start ();
22}
23}
Operation Result:
Thread-0 Sell ticket: ticket10
Thread-1 Sell ticket: ticket10
Thread-2 Sell ticket: ticket10
Thread-1 Sell ticket: TICKET9
Thread-0 Sell ticket: TICKET9
Thread-1 Sell ticket: Ticket8
Thread-2 Sell ticket: TICKET9
Thread-1 Sell ticket: TICKET7
Thread-0 Sell ticket: Ticket8
Thread-1 Sell ticket: Ticket6
Thread-2 Sell ticket: Ticket8
Thread-1 Sell ticket: TICKET5
Thread-0 Sell ticket: TICKET7
Thread-1 Sell ticket: TICKET4
Thread-2 Sell ticket: TICKET7
Thread-1 Sell ticket: Ticket3
Thread-0 Sell ticket: Ticket6
Thread-1 Sell ticket: Ticket2
Thread-2 Sell ticket: Ticket6
Thread-2 Sell ticket: TICKET5
Thread-2 Sell ticket: TICKET4
Thread-1 Sell ticket: Ticket1
Thread-0 Sell ticket: TICKET5
Thread-2 Sell ticket: Ticket3
Thread-0 Sell ticket: TICKET4
Thread-2 Sell ticket: Ticket2
Thread-0 Sell ticket: Ticket3
Thread-2 Sell ticket: Ticket1
Thread-0 Sell ticket: Ticket2
Thread-0 Sell ticket: Ticket1
Result Description:
Mythread inherits from Thread, which is a custom thread. Each mythread will sell 10 tickets.
(02) Main thread main thread create and start 3 mythread child threads. Each sub-thread has sold 10 tickets each.
2. Multithreaded examples of runnableTOEFL Answers www.jszdsy.com
Below, we make changes to the above program. Implement an interface through Runnable, which enables multithreading.
1//Runnabletest.java source
2 class MyThread implements runnable{
3 private int ticket=10;
4 public void Run () {
5 for (int i=0;i<20;i++) {
6 if (this.ticket>0) {
7 System.out.println (Thread.CurrentThread (). GetName () + "Sell ticket: Ticket" +this.ticket--);
8}
9}
10}
11};
12
public class Runnabletest {
public static void Main (string[] args) {
MyThread mt=new MyThread ();
16
17//Start 3 threads t1,t2,t3 (they share a runnable object), and these 3 threads sell 10 tickets altogether!
Thread T1=new thread (MT);
Thread T2=new thread (MT);
Thread T3=new thread (MT);
T1.start ();
T2.start ();
T3.start ();
24}
25}
Operation Result:
Thread-0 Sell ticket: ticket10
Thread-2 Sell ticket: Ticket8
Thread-1 Sell ticket: TICKET9
Thread-2 Sell ticket: Ticket6
Thread-0 Sell ticket: TICKET7
Thread-2 Sell ticket: TICKET4
Thread-1 Sell ticket: TICKET5
Thread-2 Sell ticket: Ticket2
Thread-0 Sell ticket: Ticket3
Thread-1 Sell ticket: Ticket1
Result Description:
(01) different from the above "mythread inherit from thread"; Here the Mythread implements the thread interface.
(02) The main thread, main, creates and starts 3 sub-threads, and the 3 sub-threads are created based on the Mt Runnable object. The result is that the 3 sub-threads sold a total of 10 tickets. This means that they are sharing the Mythread interface.
Java Multithreading: Two common ways to implement multithreading