There are two ways to create Java threads __java

Source: Internet
Author: User
Tags thread class ticket

There are two ways to create Java threads:

1. By inheriting the thread class, overriding the thread's run () method, where the threads run logically

2. Instantiate the thread class by implementing the Runnable interface

In practical applications, we often use multiple threads, such as the station's ticketing system, the station's various ticket outlets equivalent to each thread. When we do this the system may think of two ways to implement, inherit the thread class or implement the Runnable interface, now look at the two ways to achieve the two kinds of results.

Java Code

1. package com.threadtest;

2. class Mythread extends thread{

3.

4. private int ticket = 10;

5. private String name;

6. public mythread (String name) {

7. this. Name =name;

8.}

9.

Public void run () {

For ( int i =0;i<500;i++) {

if(this.ticket>0) {

System.out.println (this.name+ "ticket---->" + (this. ticket--));

14.}

15.}

16.}

17.}

Publicclass Threaddemo {

19.

20.

Public static void main (string[] args) {

Mythread mt1= New mythread ("one Window");

Mythread mt2= New mythread ("Window No. second");

Mythread mt3= New mythread ("window Third");

Mt1.start ();

Mt2.start ();

Mt3.start ();

28.}

29.

30.}

The results of the operation are as follows:

Java Code

1. A window to sell tickets---->10

2. A window to sell tickets---->9

3. Window Second sells tickets---->10

4. A window to sell tickets---->8

5. A window to sell tickets---->7

6. A window to sell tickets---->6

7. Window Third sells tickets---->10

8. A window to sell tickets---->5

9. A window to sell tickets---->4

10. A window to sell tickets---->3

11. A window to sell tickets---->2

12. A window to sell tickets---->1

13. Window Second sells tickets---->9

14. Window Second sells tickets---->8

15. Window Third sells tickets---->9

16. Window Third sells tickets---->8

17. Window Third sells tickets---->7

18. Window Third sells tickets---->6

19. Window Third sells tickets---->5

20. Window Third sells tickets---->4

21. Window Third sells tickets---->3

22. Window Third sells tickets---->2

23. Window Third sells tickets---->1

24. Window Second sells tickets---->7

25. Window Second sells tickets---->6

26. Window Second sells tickets---->5

27. Window Second sells tickets---->4

28. Window Second sells tickets---->3

29. Window Second sells tickets---->2

30. Window Second sells tickets---->1

The code that implements the Runnable interface is as follows:

Java Code

1. package com.threadtest;

2. class MyThread1 implements runnable{

3. private int ticket = 10;

4. private String name;

5. Public void run () {

6. for (int i =0;i<500;i++) {

7. if(this. ticket>0) {

8. System.out.println (Thread.CurrentThread (). GetName () + "ticket---->" + (This. ticket--));

9.}

10.}

11.}

12.}

Publicclass Runnabledemo {

14.

15.

Public static void main (string[] args) {

//TODO auto-generated method stub

18.//Design Three threads

MyThread1 MT = new MyThread1 ();

Thread T1 = new thread (MT, "one Window");

Thread t2 = new thread (MT, No. second window);

thread t3 = new thread (MT, No. third window);

23.//MyThread1 mt2 = new MyThread1 ();

24.//MyThread1 mt3 = new MyThread1 ();

T1.start ();

T2.start ();

T3.start ();

28.}

29.

30.}

The results of the operation are as follows:

Java Code

1. A window to sell tickets---->10

2. Window Third sells tickets---->9

3. Window Third sells tickets---->7

4. Window Third sells tickets---->5

5. Window Third sells tickets---->4

6. Window Third sells tickets---->3

7. Window Third sells tickets---->2

8. Window Third sells tickets---->1

9. A window to sell tickets---->8

10. Window Second sells tickets---->6

Why is this kind of result happening? We may as well do a metaphor, in fact just the procedure,

Inherit the thread class, we are equivalent to take out three things namely three selling 10 pieces of the task divided to three windows, they do each of the things each sell each ticket to complete each task, because mythread inherit thread class, so in new Mythread created three threads while creating three objects;

Realize Runnable, the equivalent is to take out a ticket to sell 10 tasks to three people to work together to complete, new mythread equivalent to create a task, and then instantiate three thread, create three threads that arrange three windows to execute.

The diagram shows the following:



We might be confused when we first got in touch. Inherit thread class and implement Runnable interface implement multithreading, in fact, after contact we will find that this is completely two different implementations of multiple threads, one is a number of threads to complete their own tasks, one is a number of threads together to complete a task.

In fact, in the implementation of a task with multiple threads to do can also use the inheritance thread class to achieve just more trouble, generally we use the implementation of runnable interface to achieve, concise and clear.

In most cases, if you only want to override the run () method without overriding the other Thread methods, you should use the Runnable interface. This is important because a subclass should not be created for this class (Thread) unless the programmer intends to modify or enhance the basic behavior of the class.

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.