Java Primary Learning-runnable interface and synchronized code block Synchronized__java

Source: Internet
Author: User
Tags inheritance thread class
[Java]   View Plain copy/*       Demand:  implement a ticket sales program               the second way to create a thread: Implement Runnable interface          steps:       1, the definition class implements the Runnable interface       2, overrides the Run method in the Runnable interface             Store the code that the thread is running in the Run method         3, creating the Thread object      via the thread  class   4, the Runnable interface's subclass object is passed as the actual parameter to the thread class's constructor            Why would you pass a subclass object in the Runnable interface to the thread's constructor            because, The object that the custom run method belongs to is the subclass object of the Runnable interface            So let the thread specify the run method of the specified object. You must be clear about which object the Run method belongs to.         5, calling the thread class Start method   opening the thread and calling the Run method of the Runnable interface subclass         Differences in implementation and inheritance:    implementation benefits: Avoid the limitations of single inheritance   define recommendations using implementation     two ways to differentiate:  inheritance thread: Thread code is stored in ThreaD Subclass the Run method.   Implementation runnable: Thread code stored in the interface of the subclass of the Run method,   */      class tick implements  runnable   {       private int tick = 50;           public void run ()        {            while (true)             {                if (tick > 0)                     system.out.println ( thread.currentthread (). GetName ()  +  " sail  --" + tick--";           }        }  }      class tickdemo   {      &nBsp; public static void main (String []args)        {            tick t = new tick ();               thread t1 = new thread (t );           thread t2 = new thread (t);            thread t3 = new thread (t);            thread t4 = new thread (t);               t1.start ();            t2.start ();            T3.start ();           t4.start ();          }  

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.