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.