Differences between Runnable and Thread in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.