Java Basics-Multi-threading-ticketing

Source: Internet
Author: User

static Thread
currentThread()
Returns a reference to the currently executing thread object.

long
getId()
Returns the identifier of the thread.

String
getName()
Returns the name of the thread.

int
getPriority()
Returns the priority of the thread.

Thread.State
getState()
Returns the state of the thread.

static void
sleep(long millis)
Lets the currently executing thread hibernate (suspends execution) within the specified number of milliseconds, which is affected by the accuracy and accuracy of the system timer and scheduler.

static void
sleep(long millis, int nanos)
Allows the currently executing thread to hibernate (suspend execution) within the specified number of milliseconds plus the specified number of nanoseconds, which is affected by the accuracy and accuracy of the system timer and scheduler.

/** *  */ PackageCom.train; Public classmthreadtest{ Public Static voidMain (string[] args) {Ticketseller tser1=NewTicketseller ("Tser1"); Ticketseller Tser2=NewTicketseller ("Tser2"); Ticketseller Tser3=NewTicketseller ("Tser3"); Ticketseller Tser4=NewTicketseller ("Tser4");        Tser1.start ();        Tser2.start ();        Tser3.start ();    Tser4.start (); }} classTicketsellerextendsthread{Private Static intTicknum = 100; Ticketseller (String name) {Super(name); }        //Override     Public voidrun () {Selltick (1); }         Public voidSelltick (intnum) {        if(Ticknum > 0) {System.out.println ( This. GetName () + "sells" +ticknum--); }Else{System.out.println ("Sell out!"); } System.out.println ("Current ticket number is" +ticknum); }}

Java Basics-Multi-threading-ticketing

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.