Java Learning Notes-multithreading (two)

Source: Internet
Author: User

Summary of common methods for Java threading

1.join () method

join--a thread to wait for another thread to finish before continuing. If a thread is called the Join () method in the thread execution body, the a thread is blocked, and until the B thread finishes executing, a will be able to continue execution.

public class Myrunnable implements runnable{    @Override public    void Run () {for        (int i=0;i<5;i++ {            System.out.println (Thread.CurrentThread (). GetName () + "" + i);        }}}
public class main {public    static void Main (string[] args) throws interruptedexception {        Runnable runnabl E = new myrunnable ();        Thread thread = new thread (runnable);        for (int i=0;i<5;i++) {            System.out.println ("Main:" +thread.currentthread (). GetName () + "" + i); if (i==2) {Thread.Start (); Thread.Join ();       }}}

Operation Result:

Main:main 0main:main 1main:main 2Thread-0 0Thread-0 1Thread-0 2Thread-0 3Thread-0 4 Main:main 3main:main 4        

2.thread.sleep () method

Sleep is a thread-class method that causes this thread to pause execution for a specified time, giving the execution an opportunity to another thread, but the monitoring State remains, and then automatically resumes after the call to sleep does not release the object lock. The synchronization method inside cannot be called because the object lock is not released.

 public class  Main {private int j = Ten ;    Object obj = new  object ();        public static void Main (string[] args) throws  interruptedexception {main main = new  main (); Runnable r1 = main.new  myrunnable (); Runnable r2 = main.new  myrunnable (); Thread T1 = new  thread (R1); Thread t2 = new  thread (R2); T1.start (); T2.start ();} class Myrunnable implements  Runnable {public void  Run () {synchronized  (obj) {for (int i = 0; i < 5; i++ ) {j + + ; System.out.println ("Thread:" +thread.currentthread () + "I:" +i+ ", J:" +j+ ", Time:" +  System.currenttimemillis () "); if (i==2 ) {try  {System.out.println ("Thread:" +thread.currentthread () + "Sleep" ); Thread.Sleep (+ ),} catch  (Interruptedexception e) {e.printstacktrace ();}}} } } }} 

Operation Result:

Thread: thread[thread-0,5,main]i:0,j:11,time:1503114191497 Thread: thread[thread-0,5,main]i:1,j:12,time:1503114191497 thread: thread[thread-0,5,main]i:2,j:13,time:1503114191497 Thread: thread[thread-0,5, main] sleep thread: thread[ thread-0,5,main]i:3,j:14,time:1503114193500 Thread: thread[thread-0,5,main]i:4,j:15,time:1503114193500  Thread: thread[thread-1,5,main]i:0,j:16,time:1503114193500 Thread: thread[thread-1,5,main]i:1,j:17,time:1503114193500 thread: thread[thread-1,5,main]i:2,j:18,time:1503114193501 Thread: thread[thread-1,5, main] sleep thread: thread[ thread-1,5,main]i:3,j:19,time:1503114195502 Thread: thread[thread-1,5,main]i:4,j:20,time:1503114195502 

From the results, Thread-0 does not release the lock after hibernation, so the Thread-1 does not execute until the Thread-0 sleep is finished and the Thread-1 is executed.

3.thread.yield () method

Invoking the yield method causes the current thread to hand over the CPU permissions, allowing the CPU to execute other threads. It is similar to the sleep method and does not release the lock. However, yield does not control the specific time to hand over the CPU, and the yield method only allows threads with the same priority to have the opportunity to get CPU execution time.

Note that calling the yield method does not cause the thread to go into a blocking state, but instead allows the thread to return to the ready state, which only needs to wait for the CPU execution time to be fetched again, which is not the same as the sleep method.

public class main {public    static void Main (string[] args) throws interruptedexception {        main main = new
     
       Main ();        Thread T1 = main.new
       MyThread ();        T1.start ();    }    Class MyThread extends thread{public        void Run () {long begintime=system.currenttimemillis (); int Count=0; for (int i=0;i<50000000;i++) {count=count+ (i+1); Thread.yield (); } Long endtime=system.currenttimemillis (); System.out.println ("spents:" + (Endtime-begintime) + "milliseconds! "); } }}
     

Operation Result:

Spents: 4448 milliseconds!

Put Thread.yield (); Comment out, run the result:

Spents: 1 milliseconds!

4.getPriority and SetPriority

Used to get and set thread priority.

Thread T1 = new thread (); t1.setpriority (thread.max_priority);

Max_priority:10

Min_priority:1

Norm_priority:5

5.setDaemon and Isdaemon

Used to set whether the thread is a daemon thread and whether the thread is a daemon thread.

Thread T1 = new thread (); T1.setdaemon (true);

The daemon thread (the background thread) primarily serves other threads. such as garbage collection threads in the JVM.

The life cycle of a daemon thread is associated with the foreground thread life cycle. This is mainly reflected in: when all foreground threads are in the dead state, the background thread will die automatically.

Java Learning Notes-multithreading (two)

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.