Home >Java >javaTutorial >Detailed explanation of the use of multi-threading in Java
This article mainly introduces relevant information about the detailed usage of Java multi-threading. I hope this article can help everyone. Friends in need can refer to the following
Detailed introduction of the usage of Java multi-threading
The most comprehensive analysis of Java multi-threading usage. If you have not done in-depth research on Java's multi-threading mechanism, this article can help you more thoroughly understand the principles and usage of Java multi-threading.
1. Create a thread
There are two ways to create a thread in Java: using the Thread class and using the Runnable interface. When using the Runnable interface, you need to create a Thread instance. Therefore, whether you create a thread through the Thread class or the Runnable interface, you must create an instance of the Thread class or its subclass. Thread constructor:
public Thread( ); public Thread(Runnable target); public Thread(String name); public Thread(Runnable target, String name); public Thread(ThreadGroup group, Runnable target); public Thread(ThreadGroup group, String name); public Thread(ThreadGroup group, Runnable target, String name); public Thread(ThreadGroup group, Runnable target, String name, long stackSize);
Method one: Inherit the Thread class and override the run method
public class ThreadDemo1 { public static void main(String[] args){ Demo d = new Demo(); d.start(); for(int i=0;i<60;i++){ System.out.println(Thread.currentThread().getName()+i); } } } class Demo extends Thread{ public void run(){ for(int i=0;i<60;i++){ System.out.println(Thread.currentThread().getName()+i); } } }
Method two:
public class ThreadDemo2 { public static void main(String[] args){ Demo2 d =new Demo2(); Thread t = new Thread(d); t.start(); for(int x=0;x<60;x++){ System.out.println(Thread.currentThread().getName()+x); } } } class Demo2 implements Runnable{ public void run(){ for(int x=0;x<60;x++){ System.out.println(Thread.currentThread().getName()+x); } } }
2. The life cycle of a thread
Just like people’s birth, old age, illness and death, threads also have to go through starting (waiting), running, There are four different states of suspend and stop. These four states can be controlled through methods in the Thread class. The methods related to these four states in the Thread class are given below.
// 开始线程 publicvoid start( ); publicvoid run( ); // 挂起和唤醒线程 publicvoid resume( ); // 不建议使用 publicvoid suspend( ); // 不建议使用 publicstaticvoid sleep(long millis); publicstaticvoid sleep(long millis, int nanos); // 终止线程 publicvoid stop( ); // 不建议使用 publicvoid interrupt( ); // 得到线程状态 publicboolean isAlive( ); publicboolean isInterrupted( ); publicstaticboolean interrupted( ); // join方法 publicvoid join( ) throws InterruptedException;
The thread does not execute the code in the run method immediately after it is established, but is in a waiting state. When the thread is in the waiting state, you can set various attributes of the thread through the methods of the Thread class, such as the thread's priority (setPriority), thread name (setName), and thread type (setDaemon).
When the start method is called, the thread starts executing the code in the run method. The thread enters the running state. You can use the isAlive method of the Thread class to determine whether the thread is running. When the thread is in the running state, isAlive returns true. When isAlive returns false, the thread may be in the waiting state or in the stopped state. The following code demonstrates the switching between the three states of thread creation, running and stopping, and outputs the corresponding isAlive return value.
Once the thread starts executing the run method, it will not exit until the run method is completed. However, during the execution of the thread, there are two methods that can be used to temporarily stop the thread execution. These two methods are suspend and sleep. After using suspend to suspend a thread, you can wake it up through the resume method. After using sleep to make the thread sleep, the thread can only be in the ready state after the set time (after the thread sleep ends, the thread may not execute immediately, but only enters the ready state, waiting for the system to schedule).
There are two points to note when using the sleep method:
1. The sleep method has two overloaded forms. One of the overloaded forms can not only set milliseconds, but also set nanoseconds. (1,000,000 nanoseconds equals 1 millisecond). However, the Java virtual machine on most operating system platforms is not accurate to nanoseconds. Therefore, if nanoseconds are set for sleep, the Java virtual machine will take the millisecond closest to this value.
2. When using the sleep method, throws or try{…}catch{…} must be used. Because the run method cannot use throws, you can only use try{…}catch{…}. When the thread is sleeping and the interrupt method is used to interrupt the thread, sleep will throw an InterruptedException. The definition of the sleep method is as follows:
publicstaticvoid sleep(long millis) throws InterruptedException publicstaticvoid sleep(long millis, int nanos) throws InterruptedException
There are three ways to terminate the thread.
1. Use the exit flag to make the thread exit normally, that is, the thread terminates when the run method is completed.
2. Use the stop method to forcefully terminate the thread (this method is not recommended because stop, like suspend and resume, may also produce unpredictable results).
3. Use the interrupt method to interrupt the thread.
1. Use the exit flag to terminate the thread
When the run method is executed, the thread will exit. But sometimes the run method never ends. For example, use threads in server programs to monitor client requests, or other tasks that require cyclic processing. In this case, these tasks are usually placed in a loop, such as a while loop. If you want the loop to run forever, you can use while(true){…} to handle it. But if you want to make the while loop exit under certain conditions, the most direct way is to set a boolean type flag, and set this flag to true or false to control whether the while loop exits. An example of terminating a thread using the exit flag is given below.
The function of the join method is to make asynchronous execution threads become synchronous execution. That is to say, when the start method of the thread instance is called, this method will return immediately. If you need to use a value calculated by this thread after calling the start method, you must use the join method. If you do not use the join method, there is no guarantee that when a statement following the start method is executed, the thread will be executed. After using the join method, the program will not continue execution until this thread exits. The following code demonstrates the use of join.
3. Multi-thread security issues
问题原因:当多条语句在操作同一个线程共享数据时,一个线程对多条语句只执行了一部分,还没执行完,另一个线程参与进来执行,导致共享数据的错误。
解决办法:对多条操作共享数据的语句,只能让一个线程都执行完,在执行过程中,其他线程不执行。
同步代码块:
public class ThreadDemo3 { public static void main(String[] args){ Ticket t =new Ticket(); Thread t1 = new Thread(t,"窗口一"); Thread t2 = new Thread(t,"窗口二"); Thread t3 = new Thread(t,"窗口三"); Thread t4 = new Thread(t,"窗口四"); t1.start(); t2.start(); t3.start(); t4.start(); } } class Ticket implements Runnable{ private int ticket =400; public void run(){ while(true){ synchronized (new Object()) { try { Thread.sleep(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(ticket<=0) break; System.out.println(Thread.currentThread().getName()+"---卖出"+ticket--); } } } }
同步函数
public class ThreadDemo3 { public static void main(String[] args){ Ticket t =new Ticket(); Thread t1 = new Thread(t,"窗口一"); Thread t2 = new Thread(t,"窗口二"); Thread t3 = new Thread(t,"窗口三"); Thread t4 = new Thread(t,"窗口四"); t1.start(); t2.start(); t3.start(); t4.start(); } } class Ticket implements Runnable{ private int ticket = 4000; public synchronized void saleTicket(){ if(ticket>0) System.out.println(Thread.currentThread().getName()+"卖出了"+ticket--); } public void run(){ while(true){ saleTicket(); } } }
同步函数锁是this 静态同步函数锁是class
线程间的通信
public class ThreadDemo3 { public static void main(String[] args){ class Person{ public String name; private String gender; public void set(String name,String gender){ this.name =name; this.gender =gender; } public void get(){ System.out.println(this.name+"...."+this.gender); } } final Person p =new Person(); new Thread(new Runnable(){ public void run(){ int x=0; while(true){ if(x==0){ p.set("张三", "男"); }else{ p.set("lili", "nv"); } x=(x+1)%2; } } }).start(); new Thread(new Runnable(){ public void run(){ while(true){ p.get(); } } }).start(); } } /* 张三....男 张三....男 lili....nv lili....男 张三....nv lili....男 */
修改上面代码
public class ThreadDemo3 { public static void main(String[] args){ class Person{ public String name; private String gender; public void set(String name,String gender){ this.name =name; this.gender =gender; } public void get(){ System.out.println(this.name+"...."+this.gender); } } final Person p =new Person(); new Thread(new Runnable(){ public void run(){ int x=0; while(true){ synchronized (p) { if(x==0){ p.set("张三", "男"); }else{ p.set("lili", "nv"); } x=(x+1)%2; } } } }).start(); new Thread(new Runnable(){ public void run(){ while(true){ synchronized (p) { p.get(); } } } }).start(); } } /* lili....nv lili....nv lili....nv lili....nv lili....nv lili....nv 张三....男 张三....男 张三....男 张三....男 */
等待唤醒机制
/* *线程等待唤醒机制 *等待和唤醒必须是同一把锁 */ public class ThreadDemo3 { private static boolean flags =false; public static void main(String[] args){ class Person{ public String name; private String gender; public void set(String name,String gender){ this.name =name; this.gender =gender; } public void get(){ System.out.println(this.name+"...."+this.gender); } } final Person p =new Person(); new Thread(new Runnable(){ public void run(){ int x=0; while(true){ synchronized (p) { if(flags) try { p.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }; if(x==0){ p.set("张三", "男"); }else{ p.set("lili", "nv"); } x=(x+1)%2; flags =true; p.notifyAll(); } } } }).start(); new Thread(new Runnable(){ public void run(){ while(true){ synchronized (p) { if(!flags) try { p.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }; p.get(); flags =false; p.notifyAll(); } } } }).start(); } }
生产消费机制一
public class ThreadDemo4 { private static boolean flags =false; public static void main(String[] args){ class Goods{ private String name; private int num; public synchronized void produce(String name){ if(flags) try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.name =name+"编号:"+num++; System.out.println("生产了...."+this.name); flags =true; notifyAll(); } public synchronized void consume(){ if(!flags) try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("消费了******"+name); flags =false; notifyAll(); } } final Goods g =new Goods(); new Thread(new Runnable(){ public void run(){ while(true){ g.produce("商品"); } } }).start(); new Thread(new Runnable(){ public void run(){ while(true){ g.consume(); } } }).start(); } }
生产消费机制2
public class ThreadDemo4 { private static boolean flags =false; public static void main(String[] args){ class Goods{ private String name; private int num; public synchronized void produce(String name){ while(flags) try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.name =name+"编号:"+num++; System.out.println(Thread.currentThread().getName()+"生产了...."+this.name); flags =true; notifyAll(); } public synchronized void consume(){ while(!flags) try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"消费了******"+name); flags =false; notifyAll(); } } final Goods g =new Goods(); new Thread(new Runnable(){ public void run(){ while(true){ g.produce("商品"); } } },"生产者一号").start(); new Thread(new Runnable(){ public void run(){ while(true){ g.produce("商品"); } } },"生产者二号").start(); new Thread(new Runnable(){ public void run(){ while(true){ g.consume(); } } },"消费者一号").start(); new Thread(new Runnable(){ public void run(){ while(true){ g.consume(); } } },"消费者二号").start(); } } /* 消费者二号消费了******商品编号:48049 生产者一号生产了....商品编号:48050 消费者一号消费了******商品编号:48050 生产者一号生产了....商品编号:48051 消费者二号消费了******商品编号:48051 生产者二号生产了....商品编号:48052 消费者二号消费了******商品编号:48052 生产者一号生产了....商品编号:48053 消费者一号消费了******商品编号:48053 生产者一号生产了....商品编号:48054 消费者二号消费了******商品编号:48054 生产者二号生产了....商品编号:48055 消费者二号消费了******商品编号:48055 */
The above is the detailed content of Detailed explanation of the use of multi-threading in Java. For more information, please follow other related articles on the PHP Chinese website!