Java Threading--interrupt Join yield Setdaemon use of common methods

Source: Internet
Author: User

Concept:

The operating system can have multiple processes, and one thread can have one or more threads. The processes and processes do not share memory and are run in their own space. The thread can not only share the memory, but also can use a own memory space, called the line stacks.

Threads are also called lightweight processes. Java threads are implemented by fast switching of CPU time slices, not really at the same time. Each thread has its own line stacks and program counter. Threads store variables in their own stacks so that data does not interfere with each other, and for common resources, it is easy to cause concurrency problems and need to be processed synchronously.

There are two common ways to create threads:

1. Inherit the thread class, rewrite run ()

2. Implement Runnable interface

Thread life cycle: Create-ready-run-block-death

Note

1, thread scheduling based on the priority priority principle on the basis of time slices, note that even if setpriority () is not necessarily executed exactly in sequence

2, the thread enters the runnable (can run the state, the readiness state), does not mean that it executes immediately.

jion: Call Other threads. The current thread calls the Jion () method of another thread, leaving itself in the interrupt state, and must wait for the calling thread to finish before it can enter a ready state (not run immediately).

Code

 Public classTest { Public Static voidMain (string[] args) {MyThread m=NewMyThread (); Thread T=NewThread (m);        T.start (); intI=0;  while(i++<100){            if(i==50){                Try{t.join (); } Catch(interruptedexception e) {e.printstacktrace (); }} System.out.println ("Main:" +i); }    }}classMyThreadImplementsrunnable{intI=0;  Public voidrun () { while(i++<50){            Try{Thread.Sleep (10); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ("My Thread:" +i); }    }    }

Yield: pauses the current thread, yield () is a static method, and the function is to pause the currently executing thread object and execute other threads. The current thread becomes ready, meaning that a thread may also run this thread after it calls the yield () method (it behaves as if it is not paused) (only personal view, not yet seen where there is a description of this, there may be misunderstandings).

Code

 Public classTest { Public Static voidMain (string[] args) {Thread T1=NewMyThread ();         T1.start ();  for(inti=0; i<100; i++) {System.out.println (i); }    } } classMyThreadextendsThread { Public voidrun () { for(inti = 0; I < 100; i++) {System.out.println ("MyThread" +i);        Thread.yield (); }     } } 

Interrupt: Middle Break thread. the thread will be set to the interrupt state, which is set to true. The thread periodically detects the interrupt identifier bit to determine if the thread should be interrupted. A thread can invoke the interrupt method of another thread , which issues a interruptedexception to the suspended thread .

isinterrupted : Determines whether the thread is interrupted and does not clear the interrupt state

interruped : Returns the last interrupt state of the thread and clears the interrupt flag bit, which is reset to False

What actions can cause a thread to go into a blocking state?

  Ji on(), yield, wait (), sleep (), I/O

Java Threading--interrupt Join yield Setdaemon use of common methods

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.