Multi-threading can be implemented by inheriting Thread and implementing Runable. The following example shows the differences between the two:
1. inherit the Thread class multithreading (the Thread is also a subclass of the Runnable interface)
Public class MyThread1 extends Thread {private int ticket = 10; public void run () {for (int I = 0; I <20; I ++) {if (this. ticket> 0) {System. out. println ("selling tickets: ticket" + this. ticket --) ;}} public static void main (String [] args) {MyThread1 mt1 = new MyThread1 (); MyThread1 mt2 = new MyThread1 (); myThread1 mt3 = new MyThread1 (); mt1.start (); // each thread sells 10 tickets, with a total of 30 tickets mt2.start (); // but there are actually only 10 tickets. Each thread sells its own mt3.start (); // Resource Sharing Failed }}}
Output:
Ticket Selling: ticket10
Ticket Selling: ticket9
Ticket Selling: ticket8
Ticket Selling: ticket7
Ticket Selling: ticket6
Ticket Selling: ticket5
Ticket Selling: ticket4
Ticket Selling: ticket3
Ticket Selling: ticket2
Ticket Selling: ticket1
Ticket Selling: ticket10
Ticket Selling: ticket9
Ticket Selling: ticket8
Ticket Selling: ticket7
Ticket Selling: ticket6
Ticket Selling: ticket5
Ticket Selling: ticket4
Ticket Selling: ticket3
Ticket Selling: ticket2
Ticket Selling: ticket1
Ticket Selling: ticket10
Ticket Selling: ticket9
Ticket Selling: ticket8
Ticket Selling: ticket7
Ticket Selling: ticket6
Ticket Selling: ticket5
Ticket Selling: ticket4
Ticket Selling: ticket3
Ticket Selling: ticket2
Ticket Selling: ticket1
2. Implement the Runnable interface
Public class TestRunable implements Runnable {private int ticket = 10; @ Overridepublic void run () {// TODO Auto-generated method stubfor (int I = 0; I <20; I ++) {if (this. ticket> 0) {System. out. println ("selling tickets: ticket" + this. ticket --) ;}} public static void main (String [] args) {TestRunable mt = new TestRunable (); new Thread (mt ). start (); // The same mt, but not in the Thread. If the same new Thread (mt) is used ). start (); // an instantiated object mt, and an exception new Thread (mt) will occur ). start ();}}
Output:
Ticket Selling: ticket10
Ticket Selling: ticket9
Ticket Selling: ticket8
Ticket Selling: ticket7
Ticket Selling: ticket5
Ticket Selling: ticket3
Ticket Selling: ticket2
Ticket Selling: ticket1
Ticket Selling: ticket6
Ticket Selling: ticket4
Summarize the differences between the two:
· One class can implement multiple interfaces, but only one class can be inherited
· Runnable allows Resource Sharing
Additional questions:
public class MyThread implements Runnable{@Overridepublic void run() {// TODO Auto-generated method stubSystem.out.println("run");throw new RuntimeException("Problem");}public static void main(String[] args) {Thread thread = new Thread(new MyThread());thread.start();System.out.println("End of method");}}
Which two can be results? (Choose two .)
A. java. lang. RuntimeException: Problem
B. run
Java. lang. RuntimeException: Problem
C. End of method.
Java. lang. RuntimeException: Problem
D. End of method.
Run.
Java. lang. RuntimeException: Problem
E. run
Java. lang. RuntimeException: Problem
End of method