When I think my code is safe, wait...
catalogue
Basic concepts of multithreading
Basic concepts: program, process and thread
Understanding of single core CPU and multi-core CPU
Advantages of using multithreading
Method 1 of creating multithreading
Code example of implementing multithreading by inheritance:
Supplement: classification of threads
Method 2 of creating multithreading
Implementation method multithreading code example:
Comparison of two ways to realize multithreading
Case analysis: ticket selling at railway station
Analyze the synchronization principle:
Lock in synchronization mechanism
Operation that does not release the lock
Method 1: synchronize code blocks
Thread synchronization in inheritance mode
Thread synchronization of implementation mode
Mode 2: synchronization method
Thread synchronization of implementation mode
Thread synchronization in inheritance mode
Summary of synchronization methods
Thread safe single instance lazy
wait() and notify() and notifyAll()
Classic case of thread communication:
Three ways to create multithreading
Four ways to create multithreading
Benefits of using thread pools
Basic concepts of multithreading
Basic concepts: program, process and thread
Understanding of single core CPU and multi-core CPU
Parallelism and concurrency
Advantages of using multithreading
When do I need multithreading
Creation and use of threads
Method 1 of creating multithreading
Note:
Code example of implementing multithreading by inheritance:
package com.ykx.java; /** * @author yangkx * @create 2022-01-24 17:42 */ public class HelloWorld { public static void main(String[] args) { MyThread t = new MyThread(); t.start(); for(int i = 0; i < 100; i++) if(i % 2 != 0) System.out.println(i+"=====main"); } } class MyThread extends Thread{ @Override public void run() { for(int i = 0; i < 100; i++) if(i % 2 == 0) System.out.println(i); } }
Common methods of thread
Thread scheduling
thread priority
Supplement: classification of threads
Method 2 of creating multithreading
Implementation method multithreading code example:
public class ThreadDemo { public static void main(String[] args) { //③ Create objects of this class MThread mThread = new MThread(); //④ Pass this object as a parameter to the Thread constructor to create the object Thread thread = new Thread(mThread); //⑤ Call the start() method thread.start(); } } //① Class implements the Runnable interface class MThread implements Runnable{ //Override run method @Override public void run() { for(int i = 0 ; i < 100; i++) if(i % 2 == 0 ) System.out.println(i); } }
Comparison of two ways to realize multithreading
Thread life cycle
Thread synchronization
Raising questions
Case analysis: ticket selling at railway station
Simulate train station ticket sales and open three windows for ticket sales
Ideal state
Extreme state
problem analysis
How to use Synchronized
Analyze the synchronization principle:
Lock in synchronization mechanism
Scope of synchronization
Operation of releasing lock
Operation that does not release the lock
Method 1: synchronize code blocks
Thread synchronization in inheritance mode
Code implementation:
package com.ykx.java; /** * @author: yangkx * @Title: WindowTest * @ProjectName: JavaSenior * @Description: * @date: 2022/1/27 15:57 * Multithreading of synchronous code block processing inheritance mode */ public class WindowTest1 { public static void main(String[] args) { Window1 window1 = new Window1(); Window1 window2 = new Window1(); Window1 window3 = new Window1(); window1.setName("Window one"); window2.setName("Window II"); window3.setName("Window three"); window1.start(); window2.start(); window3.start(); } } class Window1 extends Thread { private static int ticket = 20; @Override public void run() { while (true) { synchronized (Window1.class) { if (ticket > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":Ticket No.:" + ticket); ticket--; } else { break; } } } } }
Thread synchronization of implementation mode
Code implementation:
package com.ykx.java; /** * @author: yangkx * @Title: WindowTest2 * @ProjectName: JavaSenior * @Description: * @date: 2022/1/27 16:57 * Multithreading of synchronous code block processing implementation */ public class WindowTest2 { public static void main(String[] args) { Window2 window2 = new Window2(); Thread thread1 = new Thread(window2); Thread thread2 = new Thread(window2); Thread thread3 = new Thread(window2); thread1.setName("Window one"); thread2.setName("Window II"); thread3.setName("Window three"); thread1.start(); thread2.start(); thread3.start(); } } class Window2 implements Runnable { private static int ticket = 20; @Override public void run() { while (true) { synchronized (this) { if (ticket > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":Ticket No.:" + ticket); ticket--; } else { break; } } } } }
Mode 2: synchronization method
Thread synchronization of implementation mode
method:
example:
Code implementation:
package com.ykx.java; /** * @author: yangkx * @Title: WindowTest3 * @ProjectName: JavaSenior * @Description: * @date: 2022/1/27 16:57 * Multithreading of synchronous processing implementation */ public class WindowTest3 { public static void main(String[] args) { Window3 window3 = new Window3(); Thread thread1 = new Thread(window3); Thread thread2 = new Thread(window3); Thread thread3 = new Thread(window3); thread1.setName("Window one"); thread2.setName("Window II"); thread3.setName("Window three"); thread1.start(); thread2.start(); thread3.start(); } } class Window3 implements Runnable { private static int ticket = 20; @Override public void run() { while (true) { if(ticket > 0) show(); else break; } } private synchronized void show(){ if (ticket > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":Ticket No.:" + ticket); ticket--; } } }
Thread synchronization in inheritance mode
Because the inheritance method creates many different objects, this is different. After adding static, the synchronization monitor is class
Code implementation:
package com.ykx.java; /** * @author: yangkx * @Title: WindowTest4 * @ProjectName: JavaSenior * @Description: * @date: 2022/1/27 16:58 * The synchronous method deals with multithreading of inheritance mode */ public class WindowTest4 { public static void main(String[] args) { Window4 window1 = new Window4(); Window4 window2 = new Window4(); Window4 window3 = new Window4(); window1.setName("Window one"); window2.setName("Window II"); window3.setName("Window three"); window1.start(); window2.start(); window3.start(); } } class Window4 extends Thread{ private static int ticket = 20; @Override public void run() { while(true){ if(ticket > 0) show(); else break; } } private static synchronized void show(){ if (ticket > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":Ticket No.:" + ticket); ticket--; } } }
Summary of synchronization methods
Thread safe single instance lazy
Thread deadlock
Method 3: Lock
synchronized vs Lock
Thread communication
wait() and notify() and notifyAll()
sleep() and wait()
Classic case of thread communication:
Three ways to create multithreading
Implement Callable interface
Four ways to create multithreading
Use thread pool
Benefits of using thread pools