Threads in Java (ii)

Source: Internet
Author: User

1. The second way to implement threading

Provides a class that implements the interface runnable as the target object of the thread, when initializing a thread object of a thread class, or threads subclass, to pass the target object to this thread instance, provided by the target object to the thread body

Class Runnableimpl implements Runnable{public void Run () {for (int i = 0; i <, i + +;) {System.out.println ("Runnable --"+ i);}}}

Class Test{public static void Main (String args[]) {//generates an object for the Runnable interface implementation class Runnableimpl RI = new Runnableimpl ();// Generates a thread object and passes the object of the Runnable interface implementation class as a parameter to the thread object thread t = new thread (),//Notifies the thread object, executes the Start Method T.start ();}}

In the actual development process more inclined to the second approach, because the first inheritance thread has only one chance, inheritance thread can not inherit other (single inheritance)


2. Common methods for controlling threads

。 Break Thread in

--thread.sleep (); Thread.Sleep (2000); After sleep in the thread body to set a parameter (2000 milliseconds) sleep (2000) after a short period of time to run, because the thread to preempt the CPU

Test:

Class Runnableimpl implements Runnable{public void Run () {for (int i = 0; i <, i + +;) {System.out.println ("Runnable --"+ i), if (i = =) {try{thread.sleep (2000);} catch (Exception e) {System.out.println (e);}}}}

Class Test{public static void Main (String args[]) {//generates an object for the Runnable interface implementation class Runnableimpl RI = new Runnableimpl ();// Generates a thread object and passes the object of the Runnable interface implementation class as a parameter to the thread object thread t = new thread (),//Notifies the thread object, executes the Start Method T.start ();}}


--thread.yield (); Execute to this line automatically out of the CPU, and then two threads at the same time preemption CPU, it is possible or this thread to grab the CPU


。 Set the priority of a thread

--getpriority ();


--setpriority ();

Class Test{public static void Main (String args[]) {//generates an object for the Runnable interface implementation class Runnableimpl RI = new Runnableimpl ();// Generates a thread object and passes the object of the Runnable interface implementation class as a parameter to the thread object//The thread has a maximum priority of 10, the minimum is 1, and the thread's priority can be set using the static constants provided by thread;//<span style= " Color: #ff0000; " > Note: The higher the priority of the thread, the greater the probability of execution (note that probability is not necessarily performed first) </span>//t.setpriority (thread.max_priority);// Set maximum priority t.setpriority (thread.min_priority),//Set minimum priority Thread T = new thread (),//Notify thread object, execute Start Method T.start (); System.out.println (T.getpriority ());}}

Threads in Java (ii)

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.