当我以为我的代码很安全时,等等。。。
目录
wait() 与 notify() 和 notifyAll()
多线程的基本概念
基本概念:程序、进程、线程
单核CPU和多核CPU的理解
并行与并发
使用多线程的优点
何时需要多线程
线程的创建和使用
创建多线程的方式一
注意点:
继承方式实现多线程代码示例:
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);
}
}
线程的常用方法
线程的调度
线程的优先级
补充:线程的分类
创建多线程的方式二
实现方式实现多线程代码示例:
public class ThreadDemo {
public static void main(String[] args) {
//③创建该类的对象
MThread mThread = new MThread();
//④将此对象作为参数,传递到Thread构造器中,创建对象
Thread thread = new Thread(mThread);
//⑤调用start()方法
thread.start();
}
}
//①类实现Runnable接口
class MThread implements Runnable{
//重写run方法
@Override
public void run() {
for(int i = 0 ; i < 100; i++)
if(i % 2 == 0 )
System.out.println(i);
}
}
两种实现多线程方式的比较
线程的生命周期
线程的同步
问题的提出
案例分析:火车站售票
模拟火车站售票,开启三个窗口售票
理想状态
极端状态
问题分析
Synchronized的使用方法
分析同步原理:
同步机制中的锁
同步的范围
释放锁的操作
不会释放锁的操作
方式一:同步代码块
继承方式的线程同步处理
代码实现:
package com.ykx.java;
/**
* @author: yangkx
* @Title: WindowTest
* @ProjectName: JavaSenior
* @Description:
* @date: 2022/1/27 15:57
* 同步代码块处理继承方式的多线程
*/
public class WindowTest1 {
public static void main(String[] args) {
Window1 window1 = new Window1();
Window1 window2 = new Window1();
Window1 window3 = new Window1();
window1.setName("窗口一");
window2.setName("窗口二");
window3.setName("窗口三");
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);
ticket--;
} else {
break;
}
}
}
}
}
实现方式的线程同步处理
代码实现:
package com.ykx.java;
/**
* @author: yangkx
* @Title: WindowTest2
* @ProjectName: JavaSenior
* @Description:
* @date: 2022/1/27 16:57
* 同步代码块处理实现方式的多线程
*/
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("窗口一");
thread2.setName("窗口二");
thread3.setName("窗口三");
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);
ticket--;
} else {
break;
}
}
}
}
}
方式二:同步方法
实现方式的线程同步处理
方法:
例子:
代码实现:
package com.ykx.java;
/**
* @author: yangkx
* @Title: WindowTest3
* @ProjectName: JavaSenior
* @Description:
* @date: 2022/1/27 16:57
* 同步方法处理实现方式的多线程
*/
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("窗口一");
thread2.setName("窗口二");
thread3.setName("窗口三");
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);
ticket--;
}
}
}
继承方式的线程同步处理
因为继承方式造了多个不同对象,所以this是不一样的。加上static后同步监视器是class
代码实现:
package com.ykx.java;
/**
* @author: yangkx
* @Title: WindowTest4
* @ProjectName: JavaSenior
* @Description:
* @date: 2022/1/27 16:58
* 同步方法处理继承方式的多线程
*/
public class WindowTest4 {
public static void main(String[] args) {
Window4 window1 = new Window4();
Window4 window2 = new Window4();
Window4 window3 = new Window4();
window1.setName("窗口一");
window2.setName("窗口二");
window3.setName("窗口三");
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);
ticket--;
}
}
}
同步方法的总结
线程安全的单例懒汉式
线程的死锁问题
方法三:Lock锁
synchronized 与 Lock 的对比
线程的通信
wait() 与 notify() 和 notifyAll()
sleep()和wait()
线程通信的经典案例:
创建多线程的方式三
实现Callable接口
创建多线程的方式四
使用线程池
使用线程池的好处