Multithreading about precedence.

Source: Internet
Author: User

multi-Threading about precedence

Set different priority levels for multiple threads

Code

Package Chapter8;
Class MyThread4 extends Thread
{
public void Run ()
{
for (int i=0;i<5;i++)
{
System.out.println (i+ "" +getname () + "priority is:" +getpriority ());
}
}
}
public class ThreadExample3 {
public static void Main (string[] args)
{
MyThread4 t1=new MyThread4 ();
MyThread4 t2=new MyThread4 ();
T1.setpriority (1);
T2.setpriority (10);
T1.start ();
T2.start ();


}

}
Operation Result:
0 Thread-0 priority is: 1
0 Thread-1 priority is: 10
1 Thread-1 priority is: 10
2 Thread-1 priority is: 10
3 Thread-1 priority is: 10
4 Thread-1 priority is: 10
1 Thread-0 priority is: 1
2 Thread-0 priority is: 1
3 Thread-0 priority is: 1
4 Thread-0 priority is: 1
Photo:

There are two ways to start a thread, one is to inherit the thread class, and the other is to implement the interface runnable

Look at the following lines of code, is there a problem?

Code

Claass Mythread2 implements Runnable

{

public void Run ()

{

for (int i=0;i<5;i++)

{

System.out.println (i+ "" +getname () + "priority" +getpriority ());

}

}

}

There are bugs in these lines of code. Runnable is an interface. The method in which the interface must be implemented. and the GetName () and GetPriority () methods are not the methods inside this interface

There is also no getname () method and GetPriority () method in the Mythread2 class. Must be an error.

There are several methods in the class thread.
Class MyThread4 extends Thread
{
public void Run ()
{
for (int i=0;i<5;i++)
{
System.out.println (i+ "" +getname () + "priority is:" +getpriority ());
}
}
}

This is what can be done.

GetName () returns the name of the thread

GetPriority () returns the priority of the I thread

SetPriority () The priority of a thread

Multithreading about precedence.

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.