Java implementation Runnable Interface resource sharing __java

Source: Internet
Author: User
Tags thread class ticket

First, look at two pieces of code.

By inheriting the thread class

class mythread extends thread{               private int ticket = 10;       private  string name;       public mythread (string name) {            this.name =name;       }               public void run () {           for (int i =0;i<500;i++) {                if (this.ticket>0) {                    system.out.println ( this.name+ "Selling---->" + (this.ticket--));                }          }       }  }    public class threaddemo {                 public static void main (String[] args)  {            mythread mt1= new mythread ("one Window");            mythread mt2= new mythread ("Window No. second");            mythread mt3= new mythread ("Window No. third");            mt1.start ();            mt2.start ();           mt3.start ();        }     }   Running results:

A window to sell tickets---->10 A window to sell tickets---->9 window second selling tickets----the window to sell tickets---->8 A window to sell tickets---->7 window to sell tickets---->6 Third window to sell tickets----&G T;10 a window to sell tickets---->5 A window to sell tickets----a window to sell tickets---->3 A window to sell tickets---->2 A window to sell tickets---->1 No. Second window to sell tickets---->9 window second selling tickets- --->8 No. Third window selling tickets---->9 Third window selling tickets---->8 Third window selling tickets---->7 Third window selling tickets---->6 Third window selling tickets---->5 Third window selling tickets---->4 third windows   The mouth sells the ticket---->3 Third window sells the ticket---->2 Third number window sells the ticket---->1 Second window sells the ticket---->7 Second window sells the ticket---->6 Second window sells the ticket---->5 Second window sells the ticket---->4 Window second---->3 window second selling tickets---->2 window second selling tickets---->1


By implementing the Runnable interface

class mythread1 implements runnable{       private int  ticket =10;       private String name;        public void run () {           for (int  i =0;i<500;i++) {                if (this.ticket>0) {                    system.out.println (Thread.CurrentThread () getName () + "ticket---->" + (this.ticket--) );               }            }       }  }   public class runnabledemo {                  publIc static void main (String[] args)  {            // TODO Auto-generated method stub            //Design Three threads    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;MYTHREAD1&NBSP;MT  = new mythread1 ();            thread  t1 = new thread (MT, "one Window");             thread t2 = new thread (MT, "window Second");             thread t3 = new thread (MT, "window Third");             t1.start ();             T2.start ();            t3.start ();        }     } 
Run Result:

A window to sell tickets---->10 No. Third window selling ticket---->9 Third window selling tickets---->7 Third window selling tickets---->5 Third window selling tickets---->4 Third window selling tickets---->3 Third window selling tickets---- Window 23rd selling Tickets---->1 a window to sell tickets---->8 second window to sell tickets---->6
The difference is that, when the Runnable interface is inherited, a new Thread1 is created and a Thread1 is shared by three threads, so that three processes share the resource int ticket = 10;

MyThread1 MT1 = new MyThread1 ();    MyThread1 mt2 = new MyThread1 ();    MyThread1 mt2 = new MyThread1 ();     thread T1 = new Thread (MT1, "one Window");     Thread t2 = new Thread (mt2, "window Second"); thread t3 = new Thread (MT3, "window Third");
That way, it's the same as the result of inheriting the thread class.

To sum up, the implementation of runable in addition to avoiding the limitations of single inheritance, there is also a benefit to be able to achieve different process resource sharing, and inheritance thread does not have this function. Not this feature is the absolute difference between the two, inherited runable you can choose not to share.

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.