Java--Threading

Source: Internet
Author: User

Two ways to create thread 1, extends thread this class. 2, implements runnable this interface.

Set Daemon (); Create a background process; Tt.join ("a") means that a thread is released after it is added to the TT (a) MS;

Advantages of using runnable:

Optimize the single inheritance of Java language;

Suitable for multiple threads using the same resource situation;

Train ticket window A total of 100 tickets, with the threading simulation;

Usage:

Class threaddemo{public    static void Main (string[] args)     {        testthread tt=new  testthread ();//enable multiple threads, Multi-threaded shared resources;        new Thread (TT). Start ();         New Thread (TT). Start ();         New Thread (TT). Start ();         New Thread (TT). Start ();     }} Class Testthread implements runnable{    int tickets=100;     public void Run ()    {while        (true)        {            //system.out.println ("Run ():" +thread.currentthread (). GetName ());            while (tickets-->0)                System.out.println (Thread.CurrentThread (). GetName () + "is sailing ticket" + tickets--);        }    }}

  

Class threaddemo{public    static void Main (string[] args)     {        new Testthread (). Start ();  Enable multiple threads, each thread resource independent;        new Testthread (). Start ();        New Testthread (). Start ();        New Testthread (). Start ();                 /*testthread tt=new testthread ();        Tt.start ();        Tt.start ();        Tt.start ();        Tt.start (); */    }}class testthread extends thread{    int tickets=100;     public void Run ()    {while        (true)        {            //system.out.println ("Run ():" +thread.currentthread (). GetName ());            while (tickets-->0)                System.out.println (Thread.CurrentThread (). GetName () + "is sailing ticket" + tickets--);        }    }}

  

Java--Threading

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.