Java Multithreading Other

Source: Internet
Author: User

1.interrupt () method

The interrupt method does not really interrupt the thread, it only knows the blocked state of the threads ' Wait,sleep,join, and when the thread re-obtains the CPU's execution rights.

At this point, if the thread's Wait,sleep,join method is called again, a Interruptedexception exception will be thrown

Package Threadinterrupt;public class Interruptdemo {public static void main (string[] args) throws Interruptedexception {T Hread t = new Thread (new Runnable () {private Boolean flag = true; @Overridepublic void Run () {synchronized (this) {int i = 1;while (flag) {System.out.println ("=========" + i++ + "========="), if (i >) {try {System.out.println ("Child thread will stop execution"); =0;wait ();} catch (Interruptedexception e) {System.out.println ("The second call to wait () throws an exception"); E.printstacktrace (); break;}}}}); T.start (); Thread.Sleep (+); T.interrupt (); SYSTEM.OUT.PRINTLN ("Child threads will continue to execute");}}

  

2.join () method, the T1.join method represents that the current thread abandons CPU execution and needs to wait until T1 execution is complete before it can qualify for CPU execution

Package Threadinterrupt;import Java.lang.thread.state;public class Threadjoin {public static void main (string[] args) Throws Interruptedexception {Runnable r = new  Runnable () {@Overridepublic void Run () {for (int i = 0; i <; i++ {System.out.println (Thread.CurrentThread (). GetName () + ": ========" +i+ "========");}}; thread T1 = new Thread (r); Thread t2 = new Thread (r); T1.start (); T2.start ()//system.out.println ("The main thread needs to wait until T1 execution is complete");//t1.join (); if ( T1.getstate () = = state.runnable) {System.out.println ("" Without "t1.join () This sentence will be executed");} if (t1.getstate () = = state.terminated) {System.out.println ("" "With" t1.join () This sentence will be executed ");}}}

  

3.yield () method, pauses the currently executing thread object, and executes another thread, which does not let the thread discard the CPU execution, the line Cheng Jinghan can compete for CPU execution

Package Threadinterrupt;public class Threadyield {public static void main (string[] args) {Runnable r = new Runnable () {@Ov erridepublic void Run () {for (int i = 0; i <; i++) {System.out.println (Thread.CurrentThread (). GetName () + ": ======= = "+ i +" ======== "); Thread.yield ();}}; thread T1 = new Thread (r); Thread t2 = new Thread (r); System.out.println ("Add Thread.yield () to the Run method of the child thread, two threads will alternate (not absolute)"); T1.start (); T2.start ();}}

  

4.setDaemon(boolean on)方法,将该线程标记为守护线程或用户线程。

5.setPriority(int newPriority) 更改线程的优先级(1-10)数字越大,优先级越高

Java Multithreading Other

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.