Today I looked at the Java concurrent program, wrote an entry program, and set the priority of the thread.
class Elem implements Runnable{ public static int id = 0; private int cutDown = 5; private int priority; public void setPriority(int priority){ this.priority = priority; } public int getPriority(){ return this.priority; } public void run(){ Thread.currentThread().setPriority(priority); int threadId = id++; while(cutDown--> 0){ double d = 1.2; while(d < 10000) d = d + (Math.E + Math.PI)/d; System.out.println("#" + threadId + "(" + cutDown + ") "); } }}public class Basic { public static void main(String args[]){ for(int i = 0; i < 10; i++){ Elem e = new Elem(); if(i == 0 ) e.setPriority(Thread.MAX_PRIORITY); else e.setPriority(Thread.MIN_PRIORITY); Thread t = new Thread(e); t.start(); } }}
Because the machine is very powerful, I didn't see the concurrent effect at first. It felt like it was executed in sequence, so floating point operations were added in the middle to delay the time.
Of course, Executors can be used to manage threads in the main function.
import java.util.concurrent.*;class Elem implements Runnable{ public static int id = 0; private int cutDown = 5; private int priority; public void setPriority(int priority){ this.priority = priority; } public int getPriority( ){ return this.priority; } public void run(){ Thread.currentThread().setPriority(priority); int threadId = id++; while(cutDown-- > 0){ double d = 1.2; while(d < 10000) d = d + (Math.E + Math.PI)/d; System.out.println("#" + threadId + "(" + cutDown + ")"); } }}public class Basic { public static void main(String args[]){// for(int i = 0; i < 10; i++){// Elem e = new Elem();// if(i == 0 )// e.setPriority(Thread.MAX_PRIORITY);// else// e.setPriority(Thread.MIN_PRIORITY);/ /Thread t = new Thread(e);// t.start();// } ExecutorService exec = Executors.newCachedThreadPool(); for(int i = 0; i < 10; i++){ Elem e = new Elem(); if(i == 0 ) e.setPriority(Thread.MAX_PRIORITY); else e.setPriority(Thread .MIN_PRIORITY); exec.execute(e); } exec.shutdown(); }}