JAVA thread control, java thread

Source: Internet
Author: User

JAVA thread control, java thread

You can use the thread method to perform basic thread control. Besides the known start, run, and sleep methods, there are also isAlive, currentThread, and interrupt methods.

IsAlive: This method is used to test whether a thread is active. After the thread is started by the start method, it is Alive at any time until it is terminated. This method returns false when it is in the new or dead state.

CurrentThread: This method is a class method of the Thread class. It returns the Thread that is using CPU resources.

Interrupt: When a thread is in sleep state, a thread that occupies CPU resources can call the interrupt method to wake itself up, causing the sleeping thread to stop sleep due to an InterruptedException exception, wait for CPU resources again.

Class A implements Runnable {Thread student, teacher; A () {teacher = new Thread (this, "Professor Wang"); student = new Thread (this, "James ");} public void run () {if (Thread. currentThread () = student) {try {System. out. println (student. getName () + "sleeping, not attending lectures"); Thread. sleep (1000*60*60);} catch (InterruptedException e) {System. out. println (student. getName () + "woke up by the instructor") ;}} else if (Thread. currentThread () = teacher) {for (int I = 1; I <= 3; I ++) {System. out. println ("class! ");} Try {Thread. sleep (500);} catch (InterruptedException e) {} student. interrupt () ;}} public class BasicControlThread {public static void main (String args []) {A a = new A ();. student. start ();. teacher. start ();}}

 

In addition, there are stop and join methods.

Stop (): terminate a thread by calling the instance method stop () of the thread. After termination, the thread enters the dead State and cannot be scheduled again.

Join (): When a thread occupies CPU resources, other threads can call the join () method and the same thread. The current thread waits for the thread to call this method to end, and then queues again to wait for CPU resources to resume execution.

Class TV {float price; String name; TV (String name, float price) {this. price = price; this. name = name ;}} class ThreadJoin implements Runnable {TV TV; Thread customer, tvMaker; ThreadJoin () {customer = new Thread (this, "customer "); tvMaker = new Thread (this, "TV manufacturer");} public void run () {if (Thread. currentThread () = customer) {System. out. println (customer. getName () + "and so on" + tvMaker. getName () + "production TV"); try {tvMaker. join ();/ /Thread customer starts waiting for tvMaker to end} catch (InterruptedException e) {} System. out. println (customer. getName () + "bought a TV set" + TV. name + "Price:" + TV. price);} else if (Thread. currentThread () = tvMaker) {System. out. println (tvMaker. getName () + "start to produce TV... "); try {tvMaker. sleep (2000);} catch (InterruptedException e) {} TV = new TV ("Changhong card", 4500); System. out. println (tvMaker. getName () + "production completed !!! ") ;}} Public class JoinThread {public static void main (String args []) {ThreadJoin th = new ThreadJoin (); th. customer. start (); th. tvMaker. start ();}}

 

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.