Multithreading (daemon thread, join method, thread priority, thread Group)

Source: Internet
Author: User

Setdaemon (Boolean on):

Daemon thread (background thread), the background thread ends automatically if the foreground (display) thread ends.

With the Setdaemon (Boolean on) method, the thread must be marked as a daemon thread before the thread is opened.

Example:

classSetdaemonImplementsrunnable{ Public voidrun () { while(true) {System.out.println (Thread.CurrentThread (). GetName ()+ "... run"); }    }}classSetdaemondemo { Public Static voidMain (string[] args) {Setdaemon sd=NewSetdaemon (); Thread T1=NewThread (SD); Thread T2=NewThread (SD); //Mark T1, T2 as the daemon thread. (To make a thread a daemon, it must be marked before it is opened)T1.setdaemon (true); T2.setdaemon (true);        T1.start ();        T2.start (); //foreground main thread, if main thread end, then T1, T2 Auto End         for(inti=0;i<60;i++) {System.out.println (Thread.CurrentThread (). GetName ()+"......"+i); }    }}
Daemon Thread Example

Join ():

The Join method enables the thread to take CPU execution, and the thread that invokes the join method must wait until the thread using the Join method finishes executing to begin execution

(When a thread executes the. Join () method to the B-thread), a waits. When the b thread is executed, a will not execute.

Join can be used to temporarily join thread execution. )

Example:

classJoinImplementsrunnable{ Public voidrun () { for(inti=0; i<70; i++) {System.out.println (Thread.CurrentThread (). GetName ()+"..."+i); }    }}classJoindemo { Public Static voidMain (string[] args) {Join J=Newjoin (); Thread T1=NewThread (j); Thread T2=NewThread (j);        T1.start (); //The main thread executes the execution to the join when the T1,main thread falls into hibernation.        T1 execution is finished, main can begin execution. //at this point, only the main and T1 threads, T2 not open        Try{t1.join (); }        Catch(Interruptedexception e) {} t2.start ();  for(inti=0; i<70; i++) {System.out.println (Thread.CurrentThread (). GetName ()+"..............."+i); }    }}
Joindemo

Thread Group:

A thread is which threads open it is that group of threads, (a thread is a B thread is turned on, a thread is group B)

The Threadgroup class (the Thread group Class) provides the operation of a thread group, but is seldom used in actual development.

Thread Priority:

Priority is divided into 1 to 10, 1 min 10 max.

setpriority (int num): Sets the priority of the thread,

Max_priority: Set the highest priority, Min_priority: Set the lowest priority, norm_priority assign to the thread default priority (5).

//Set Thread Priority presentationclassSetthreadprioritydemoImplementsrunnable{ Public voidrun () { for(intnumber=0; number<60; number++) {System.out.println (Thread.CurrentThread (). GetName ()+":"+Number ); }    }}//Thread-Priority PresentationclassThreadprioritydemo { Public Static voidMain (string[] args) {Setthreadprioritydemo St=NewSetthreadprioritydemo (); Thread T1=NewThread (ST); Thread T2=NewThread (ST); Thread T3=NewThread (ST); //Print thread information by the ToString () method of thread class thread, including thread name, priority, thread groupSystem.out.println (t1.tostring ());        System.out.println (T2.tostring ());        System.out.println (T3.tostring ()); //set the priority of a thread//max_priority, min_priority, norm_priority three static constant values defined for the thread class that represent the priority of threadsT1.setpriority (thread.max_priority);//Max_priority represents the highest priority.T2.setpriority (thread.min_priority);//Min_priority represents the lowest priority 1.T3.setpriority (thread.norm_priority);//Norm_priority represents the default priority of 5. //turn on the thread. T1.start ();        T2.start ();    T3.start (); }}
Set Thread Priority presentation

Yield method:

Pauses the currently executing thread object and executes other threads.

Multithreading (daemon thread, join method, thread priority, thread Group)

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.