1. Priority of threads
Examples of thread priority usage:
package cn.galc.test;public class TestThread6 { public static void main(String args[]) { MyThread4 t4 = new MyThread4(); MyThread5 t5 = new MyThread5(); Thread t1 = new Thread(t4); Thread t2 = new Thread(t5); t1.setPriority(Thread.NORM_PRIORITY + 3);// Use the setPriority() method to set the priority of threads, and here the priority of t1 threads is set /* * Increase the priority of thread t1 by level 3 on the basis of normal priority (NORM_PRIORITY)* In this way, the execution time of t1 will be much more than that of t2* By default, the value of NORM_PRIORITY is 5 */ t1.start(); t2.start(); System.out.println("t1 thread's priority is: " + t1.getPriority()); // Use the getPriority() method to get the priority of thread and print out the priority of t1 to be 8 }}class MyThread4 implements Runnable { public void run() { for (int i = 0; i <= 1000; i++) { System.out.println("T1:" + i); } }}class MyThread5 implements Runnable { public void run() { for (int i = 0; i <= 1000; i++) { System.out.println("=================== T2:" + i); } }} As soon as the run() method ends, the thread ends.2. Thread synchronization
Examples of synchronized keyword usage:
package cn.galc.test;public class TestSync implements Runnable { Timer timer = new Timer(); public static void main(String args[]) { TestSync test = new TestSync(); Thread t1 = new Thread(test); Thread t2 = new Thread(test); t1.setName("t1");// Set the name of the t1 thread t2.setName("t2");// Set the name of the t2 thread t1.start(); t2.start(); } public void run() { timer.add(Thread.currentThread().getName()); }}class Timer { private static int num = 0; public/* synchronized */void add(String name) {// When adding synchronized when declaring a method, it means that the current object is locked during the execution of this method synchronized (this) { /* * Use synchronized(this) to lock the current object, so that there will be no problem of two different threads accessing the same object resource at the same time. Only when one thread accesses will it be the next thread's turn to access */ num++; try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(name + ": You are the " + num + "threads using timer"); } }} Thread deadlock problem:
package cn.galc.test;/*This applet simulates the problem of thread deadlock*/public class TestDeadLock implements Runnable { public int flag = 1; static Object o1 = new Object(), o2 = new Object(); public void run() { System.out.println(Thread.currentThread().getName() + "flag=" + flag); /* * After running the program, I found that the program has executed it and printed out the flag, and I will never execute the subsequent if statement again* The program died here, neither executing nor exiting*/ /* This is the thread flag=1*/ if (flag == 1) { synchronized (o1) { /* Use the synchronized keyword to lock object 01*/ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o2) { /* * Object o1 has been locked before, as long as it can lock o2, you can execute the operation of printing out 1* However, object o2 cannot be locked here, because object o1 has been locked in another thread flag=0* Although the thread that locks object o2 will sleep every 500 milliseconds, it still locks o2 when sleeping*/ System.out.println("1"); } } } } /* * Both if statements here will not be executed because they have caused the problem of thread deadlock* flag=1 This thread is waiting for flag=0 This thread is unlocking the lock of object o2, * And flag=0 This thread is also waiting for flag=1 This thread unlocking the lock of object o1* However, neither of these threads are willing to unlock the locked object, so it causes the problem of thread deadlock*/ /* This is the thread flag=0*/ if (flag == 0) { synchronized (o2) { /* Here we use synchronized to lock object o2 first */ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o1) { /* * Object o2 has been locked before. As long as it can lock o1, the operation of printing out 0 can be performed. However, object o1 cannot be locked here, because object o1 has been locked in another thread of flag=1. Although the thread that locks object o1 will sleep every 500 milliseconds, it still locks o1 when sleeping*/ System.out.println("0"); } } } } public static void main(String args[]) { TestDeadLock td1 = new TestDeadLock(); TestDeadLock td2 = new TestDeadLock(); td1.flag = 1; td2.flag = 0; Thread t1 = new Thread(td1); Thread t2 = new Thread(td2); t1.setName("thread td1"); t2.setName("thread td2"); t1.start(); t2.start(); }}It is best to solve the problem of thread deadlocking only one object, not two objects at the same time.
Producer and Consumer Questions:
package cn.galc.test;/* Example name: Producer--Consumer problem* Source file name: ProducerConsumer.java * Key points: * 1. Inconsistency of shared data/protecting critical resources* 2. Concept of Java object lock* 3. Synchronized keyword/wait() and notify() methods*/public class ProducerConsumer { public static void main(String args[]){ SyncStack stack = new SyncStack(); Runnable p=new Producer(stack); Runnable c = new Consumer(stack); Thread p1 = new Thread(p); Thread c1 = new Thread(c); p1.start(); c1.start(); }}class SyncStack{ //Implementation of stacks that support multithread synchronization operations private int index = 0; private char []data = new char[6]; public synchronized void push(char c){ if(index == data.length){ try{ this.wait(); }catch(InterruptedException e){} } this.notify(); data[index] = c; index++; } public synchronized char pop(){ if(index ==0){ try{ this.wait(); }catch(InterruptedException e){} } this.notify(); index--; return data[index]; }}class Producer implements Runnable{ SyncStack stack; public Producer(SyncStack s){ stack = s; } public void run(){ for(int i=0; i<20; i++){ char c =(char)(Math.random()*26+'A'); stack.push(c); System.out.println("produced:"+c); try{ Thread.sleep((int)(Math.random()*1000)); }catch(InterruptedException e){ } } }}class Consumer implements Runnable{ SyncStack stack; public Consumer(SyncStack s){ stack = s; } public void run(){ for(int i=0;i<20;i++){ char c = stack.pop(); System.out.println("Consumption:"+c); try{ Thread.sleep((int)(Math.random()*1000)); }catch(InterruptedException e){ } } }}The above is the full introduction to Java threads. You can combine the first article "Java must learn and learn threads (1)" to learn, hoping that it can help you.