Runnable and Thread, RunnableThread

Source: Internet
Author: User

Runnable and Thread, RunnableThread

1. Runnable is an interface. when implementing this interface, you need to reuse the run method to implement your own logic in the run method.

2. Thread is a class that actually implements the Runnable method. That is to say, when you get a Thread instance through a new Thread, it is actually to override the run method in runnable. When you create a thread instance by implementing runnable, you need to pass the runnable instance to a thread and start the thread through. start.

3. Runnable can synchronize threads, but threads cannot synchronize threads. For example, if there are 10 tickets, you can enable three threads to sell tickets at the same time, if you create a ticket selling class by inheriting the thread, three threads will each sell 10 tickets, and a total of 30 tickets will be sold. At that time, you created the ticket selling class by implementing runnable, you only need to instantiate an object, pass an object to three threads, and start three threads. You will find that only 10 threads are sold in total.

4. The following code is used to sell tickets.

Package com. shine. test;/*** sell tickets by inheriting the thread class * @ author ASUS **/public class MyThread extends Thread {public int ticks = 10; boolean flag = true; public MyThread (String string) {this. setName (string) ;}@ Override public void run () {while (flag) {saleTick () ;}} public void saleTick () {if (ticks> 0) {ticks --; System. out. println (Thread. currentThread (). getName () + ":" + ticks) ;}else {flag = false ;}}}
Package com. shine. test;/*** implement the Runnable ticketing class * @ author ASUS **/public class MyRunnable implements Runnable {public int ticks = 10; boolean flag = true; @ Override public void run () {while (flag) {saleTick () ;}} public void saleTick () {if (ticks> 0) {ticks --; System. out. println (Thread. currentThread (). getName () + ":" + ticks) ;}else {flag = false ;}}}
Package com. shine. test; public class Test {/** test * @ param args */public static void main (String [] args) {MyThread myThread1 = new MyThread ("thread1 "); myThread myThread2 = new MyThread ("thread2"); MyThread myThread3 = new MyThread ("thread3"); myThread1.start (); myThread2.start (); myThread3.start (); myRunnable myRunnable = new MyRunnable (); Thread runnable1 = new Thread (myRunnable); Thread runnable2 = new Thread (myRunnable); Thread runnable3 = new Thread (myRunnable ); values ("runnable1"); runnable2.setName ("runnable2"); runnable3.setName ("runnable3"); runnable1.start (); runnable2.start (); runnable3.start ();}}

Result:
Thread1: 9thread1: 8thread1: 7thread1: 6thread1: small: 4thread1: 3thread1: small: 9thread2: 8thread2: 7thread2: 6thread2: small: 4thread2: 3thread2: 2thread2: 1thread2: 0thread3: small: 4runnable1: 3runnable1: small: 0thread3: 8thread3: 7thread3: 6thread3: 5thread3: 4thread3: 3thread3: 2thread3: 1thread3: 0

Due to the thread Selection Problem of the system, runnable1 has sold tickets, but 30 tickets are sold if only 10 threads are sold in total.

 

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.