Java multithreading Two implementation of __ "Java Advanced"

Source: Internet
Author: User
Tags thread class ticket

There are two ways to implement multithreading in Java, one is to inherit the thread class and the other is to implement the Runnable interface.

For both implementations, each has its advantages and disadvantages. Next, compare and summarize. Both of these methods can be implemented in multithreading. here are two ways to implement


how the thread class is inherited:

Package com.zc.thread;

public class Mythread extends Thread {
	private String name;

	Public Mythread (String name) {
		super ();
		this.name = name;

	public void Run () {
		for (int i = 0; i < i++) {
			System.out.println ("thread starts:" + THIS.name + ", i=" + i);
		}
	}
}


How to implement the Runnable interface:

Package com.zc.runnable;

public class Myrunnable implements Runnable {
	private String name;

	Public myrunnable (String name) {
		this.name = name;
	}

	public void Run () {
		for (int i = 0; i < i++) {
			System.out.println ("Thread start: + THIS.name +", i= "+ i);
		}
	}
}

Multithreading can be implemented in both ways. The difference between the two approaches is basically the difference between inheriting and implementing interfaces. For the first time, the implementation of Runnable interface can realize the sharing of resources.

There are limitations to inheriting the thread class. Because classes in Java cannot inherit multiple classes, multiple interfaces can be implemented. So the realization runnable how many will have some convenience.


Because these two kinds of way, is the mixture. The thread class is still the implemented Runnable interface.



The Run method differs from the Start method:

In both of these ways of implementing multithreading, you need to rewrite the Run method. To start a thread, you must call the Start method of the class. This allows the JVM to assign a thread. In order to achieve the synchronous execution of multi-line threads. That is, the start coordinates the system's resources.


So how to achieve, the sharing of resources among multiple threads.

First write a test class of your own

Package com.zc.thread;

public class Sellthread extends thread{
	private int ticket = ten;
	@Override public
	Void Run () {(
		int i = 0; i < i++) {
			if (This.ticket > 0) {
				System.out.print ln ("Selling Tickets: Ticket" + "," + this.ticket--);
			}}
		}


This class is the operation of a ticketing point.

If there is only one conductor, then this question will not need to be discussed, this is a single thread. can be used directly.

So how to achieve the situation of three conductor? Next look at the following:

public class Tickettest {public
	static void Main (string[] args) {
		 ticketsellthread tst1=new ticketsellthread ( );
		 Ticketsellthread tst2=new ticketsellthread ();
		 Ticketsellthread tst3=new ticketsellthread ();
		 Tst1.start ();
		 Tst2.start ();
		 Tst3.start ();
	}

Whether the above situation can meet the ticket sales point of three conductor.




Obviously, the ticketing situation is not right, three of the conductor sold a total of so many votes, three people are not sharing the resources caused.


To achieve this, three conductors share a resource. Then you should use the same thread to start three threads.

public static void Main (string[] args) {
        Ticketthreadr ticketthread = new Ticketthreadr ();
        
        Thread Th1 = new Thread (ticketthread);    Thread one thread
        Th2 = new Thread (ticketthread);    Threads two thread
        th3 = new Thread (ticketthread);    Line Cheng
        
        Th1.start ();
        Th2.start ();
        Th3.start ();
    }

Next look at the results:



Although printing does not print in order, it implements resource sharing.

In contrast, when the thread of the Runnable class is implemented to start, resource sharing is achieved. It is not shared if you use the implementation method of inheritance thread to invoke the start resource.


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.