today, we focus on the analysis of the thread class. This class has some properties for information. These properties can be used to identify a thread, show the state of a thread, or control the priority of a thread.
ID: The unique identity of the thread is saved.
Name: Names of the threads are saved
Priority: Saves the thread's precedence.
Status: The state of the thread is saved. The threads in Java have the following states: New,runnable,blocked,waiting,time waiting or terminated.
Now, write a program that assigns names and priorities to 10 threads, and outputs their status information until the thread ends. Each thread calculates a multiplication table for a number.
1. Create a class named Calculator, and implement the Runnable interface.
public class Calculator implements runnable{}
2. Declare a private variable number of type int, and write the constructor of the class to set the value for the attribute.
private int number;
public Calculator (int number) {
This.number=number;
}
3. Write the Run method. This method executes the instructions for the thread we created.
public void Run () {
for (int i=1;i<=10;i++) {
System.out.printf ("%s:%d *%d =%d \ n", Thread.CurrentThread (). GetName (), number,i,i*number);
}
}
4. Write a main class named Main. Create the main () method at the same time.
public class Main {
public static void Main (string[] args) {}}
5. Create a thread array with a capacity of 10 to store threads, and create a thread.status array with a capacity of 10 to hold the state of the 10 threads running.
Thread Threads[]=new thread[10];
Thread.state Status[]=new thread.state[10];
6. Create an array of Calculator objects with a capacity of 10, set different numbers for each object, and then use them as parameters to the thread constructor to create 10 thread objects, set 5 thread priority to highest, and 5 thread priority to lowest.
for (int i=0;i<10;i++) {
threads[i]=new Thread (new Calculator (i));
if ((i%2) ==0) {
threads[i].setpriority (thread.max_priority);
}else{
threads[i].setpriority (thread.min_priority);
}
threads[i].setname ("Thread" +i);
}
7. Create a PrintWriter object that is used to write the state of the thread to the file.
Try (FileWriter file=new FileWriter (". \\data\\log.txt");
PrintWriter pw=new printwriter (file);) {
8. Write the status of these 10 threads to a file, the existing thread state is new,
for (int i=0;i<10;i++) {
Pw.println ("Main:status of Thread" +i+ ":"
+threads[i].getstate ());
Status[i]=threads[i].getstate ();
}
9. Start the execution of this 10 thread
for (int i=0;i<10;i++) {
Threads[i].start ();
}
10. Until 10 threads are complete, we can view their status. All tasks a thread's state has changed and we'll say it is written to the file
Boolean finish=false;
while (!finish) {
for (int i=0; i<10; i++) {
if (Threads[i].getstate ()!=status[i]) {
Writethreadinfo (PW, threads[i],status[i]);
Status[i]=threads[i].getstate ();
}
}
Finish=true;
for (int i=0;i<10;i++) {
finish=finish&& (Threads[i].getstate () ==state.terminated);
}
}
11. Write the Writerthreadinfo () method, which is used to write the ID, name, priority, old State and new state of the downline.
private static void Writethreadinfo (PrintWriter pw, thread thread, state state) {
pw.printf ("Main:id%d-%s\n", Thread.getid (), Thread.getname ());
pw.printf ("Main:priority:%d\n", thread.getpriority ());
pw.printf ("Main:old State:%s\n", state);
pw.printf ("Main:new State:%s\n", thread.getstate ());
pw.printf ("Main: ************************************\n");
}
12. Operation Result:
Main:status of Thread0:new
Main:status of Thread1:new
Main:status of Thread2:new
Main:status of Thread3:new
Main:status of Thread4:new
Main:status of Thread5:new
Main:status of Thread6:new
Main:status of Thread7:new
Main:status of Thread8:new
Main:status of Thread9:new
Main:id 9-thread0
Main:priority:10
Main:old state:new
Main:new state:blocked
Main: ************************************
Main:id 10-thread1
Main:priority:1
Main:old state:new
Main:new state:blocked
Main: ************************************
Main:id 11-thread2
Main:priority:10
Main:old state:new
Main:new state:blocked
Main: ************************************
Main:id 12-thread3
Main:priority:1
Main:old state:new
Main:new state:blocked
Main: ************************************
Main:id 13-THREAD4
Main:priority:10
Main:old state:new
Main:new state:runnable
Main: ************************************
Main:id 14-THREAD5
Main:priority:1
Main:old state:new
Main:new state:blocked
Main: ************************************
Main:id 15-thread6
Main:priority:10
Main:old state:new
Main:new state:blocked
Main: ************************************
Main:id 16-thread7
Main:priority:1
Main:old state:new
Main:new state:blocked
Main: ************************************
Main:id 17-thread8
Main:priority:10
Main:old state:new
Main:new state:blocked
Main: ************************************
Main:id 18-thread9
Main:priority:1
Main:old state:new
Main:new state:blocked
Main: ************************************
Main:id 10-thread1
Main:priority:1
Main:old state:blocked
Main:new state:runnable
Main: ************************************
Main:id 13-THREAD4
Main:priority:10
Main:old state:runnable
Main:new state:blocked
Main: ************************************
Main:id 9-thread0
Main:priority:10
Main:old state:blocked
Main:new state:blocked
Main: ************************************
Main:id 13-THREAD4
Main:priority:10
Main:old state:runnable
Main:new state:terminated
Main: ************************************
Main:id 14-THREAD5
Main:priority:1
Main:old state:blocked
Main:new state:blocked
Main: ************************************
Main:id 12-thread3
Main:priority:1
Main:old state:blocked
Main:new state:blocked
Main: ************************************
Main:id 11-thread2
Main:priority:10
Main:old state:blocked
Main:new state:runnable
Main: ************************************
Main:id 11-thread2
Main:priority:10
Main:old state:runnable
Main:new state:terminated
Main: ************************************
Main:id 17-thread8
Main:priority:10
Main:old state:blocked
Main:new state:blocked
Main: ************************************
Main:id 18-thread9
Main:priority:1
Main:old state:blocked
Main:new state:blocked
Main: ************************************
Main:id 16-thread7
Main:priority:1
Main:old state:blocked
Main:new state:blocked
Main: ************************************
Main:id 18-thread9
Main:priority:1
Main:old state:blocked
Main:new state:blocked
Main: ************************************
Main:id 17-thread8
Main:priority:10
Main:old state:blocked
Main:new state:runnable
Main: ************************************
Main:id 17-thread8
Main:priority:10
Main:old state:runnable
Main:new state:terminated
Main: ************************************
Main:id 12-thread3
Main:priority:1
Main:old state:blocked
Main:new state:runnable
Main: ************************************
Main:id 12-thread3
Main:priority:1
Main:old state:runnable
Main:new state:terminated
Main: ************************************
Main:id 14-THREAD5
Main:priority:1
Main:old state:blocked
Main:new state:blocked
Main: ************************************
Main:id 9-thread0
Main:priority:10
Main:old state:blocked
Main:new state:runnable
Main: ************************************
Main:id 9-thread0
Main:priority:10
Main:old state:runnable
Main:new state:terminated
Main: ************************************
Main:id 10-thread1
Main:priority:1
Main:old state:blocked
Main:new state:runnable
Main: ************************************
Main:id 15-thread6
Main:priority:10
Main:old state:blocked
Main:new state:runnable
Main: ************************************
Main:id 10-thread1
Main:priority:1
Main:old state:runnable
Main:new state:blocked
Main: ************************************
Main:id 14-THREAD5
Main:priority:1
Main:old state:blocked
Main:new state:runnable
Main: ************************************
Main:id 15-thread6
Main:priority:10
Main:old state:runnable
Main:new state:blocked
Main: ************************************
Main:id 18-thread9
Main:priority:1
Main:old state:blocked
Main:new state:blocked
Main: ************************************
Main:id 14-THREAD5
Main:priority:1
Main:old state:runnable
Main:new state:blocked
Main: ************************************
Main:id 18-thread9
Main:priority:1
Main:old state:blocked
Main:new state:blocked
Main: ************************************
Main:id 14-THREAD5
Main:priority:1
Main:old state:blocked
Main:new state:runnable
Main: ************************************
Main:id 15-thread6
Main:priority:10
Main:old state:blocked
Main:new state:blocked
Main: ************************************
Main:id 10-thread1
Main:priority:1
Main:old state:blocked
Main:new state:runnable
Main: ************************************
Main:id 15-thread6
Main:priority:10
Main:old state:blocked
Main:new state:runnable
Main: ************************************
Main:id 15-thread6
Main:priority:10
Main:old state:runnable
Main:new state:terminated
Main: ************************************
Main:id 14-THREAD5
Main:priority:1
Main:old state:blocked
Main:new state:runnable
Main: ************************************
Main:id 14-THREAD5
Main:priority:1
Main:old state:runnable
Main:new state:terminated
Main: ************************************
Main:id 18-thread9
Main:priority:1
Main:old state:blocked
Main:new state:runnable
Main: ************************************
Main:id 18-thread9
Main:priority:1
Main:old state:runnable
Main:new state:terminated
Main: ************************************
Main:id 16-thread7
Main:priority:1
Main:old state:blocked
Main:new state:runnable
Main: ************************************
Main:id 16-thread7
Main:priority:1
Main:old state:runnable
Main:new state:terminated
Main: ************************************
Main:id 10-thread1
Main:priority:1
Main:old state:blocked
Main:new state:runnable
Main: ************************************
Thread management for JAVA concurrent programming 2