Multiprocessing: can run multiple tasks (programs) in the operating system (simultaneously)
Multithreading: there are multiple sequential flows (simultaneously) executing in the same application
A thread is represented by a thread object
Method of creating a thread
1. Define a thread class that inherits Thread and overrides the method run(), which is called the thread body;
since Java only supports single inheritance, classes defined in this way cannot inherit other classes.
1. Main function thread
2. ft thread
3. Garbage collection thread
. 2. The second method of implementing threads is also a more commonly used method relative to method one.
Thread.sleep(nms);//Let the CPU wait for nms and then enter the ready state to wait for the CPU to be preempted
//When using, you need to use the try...catch structure
Thread.yield();//Let the CPU enter the ready state
Set the priority of the thread
getPriority()//Get the priority
setPriority();//Set the priority
//The maximum priority of the thread is Thread.MAX_PRIORITY = 10
Multithreading: there are multiple sequential flows (simultaneously) executing in the same application
A thread is represented by a thread object
Method of creating a thread
1. Define a thread class that inherits Thread and overrides the method run(), which is called the thread body;
since Java only supports single inheritance, classes defined in this way cannot inherit other classes.
class FirstThread extends Thread{ public void run(){ for(int i=0;i<100;i++) System.out.println("FistThread-->"+i); } } class Test{ public static void main(String args[]) { //Generate object of thread class FirstThread ft = new FirstThread(); ft.start();//Start thread //ft.run();//Don't write like this //Writing this way directly executes the contents of the run method without reflecting the thread } }There are three threads when the above code runs:
1. Main function thread
2. ft thread
3. Garbage collection thread
class Test{ public static void main(String args[]) { FirstThread ft = new FirstThread(); ft.start(); for(int i=0;i<100;i++){ System.out.println("main-->"+i); } } }After modifying the above code, the execution relationship between threads and threads can be reflected. The running results are as follows
main-->0 FistThread-->0 FistThread-->1 main-->1 FistThread-->2 main-->2 FistThread-->3 main-->3 main-->4 FistThread-->4 main-->5 main-->6 FistThread-->5 main-->7 FistThread-->6 FistThread-->7 FistThread-->8 ...Multi-threading is irregular when running, that is to say, the sequence of each run may be different and uncontrollable
. 2. The second method of implementing threads is also a more commonly used method relative to method one.
class RunnableImpl implements Runnable{ public void run()//Rewrite run { for(int i=0;i<100;i++) System.out.println("Runnable-->"+i); } } class Test{ public static void main(String args[]) { //Generate an object of the Runnable interface implementation class RunnableImpl ri = new RunnableImpl(); //Generate a Trread object and use the object of the Runnable interface implementation class as a parameter //Pass to the Thread object Thread t = new Thread(ri); //Notify the Thread object to execute the method t.start(); for(int i=0;i<100;i++){ System.out.println("main-->"+i); } } }Interrupt the thread
Thread.sleep(nms);//Let the CPU wait for nms and then enter the ready state to wait for the CPU to be preempted
//When using, you need to use the try...catch structure
Thread.yield();//Let the CPU enter the ready state
Set the priority of the thread
getPriority()//Get the priority
setPriority();//Set the priority
//The maximum priority of the thread is Thread.MAX_PRIORITY = 10
//The minimum is Thread.MIN_PRIORITY = 1; the greater the priority, the greater the probability of being executed
By Urien April 14, 2018 14:07:34