Java语法学习线程基础
大纲
- 概念
- 创建线程
- 线程终止
- 常用方法
- 用户线程和守护线程
- 线程的七大状态
- 线程的同步
- 互斥锁
- 线程死锁
- 释放锁
具体案例
1.概念

 
 
 
 
2. 创建线程

 第一种:
class Cat extends Thread {
    int time = 0;
    @Override
    public void run() {
        while (true) {
            System.out.println("只不过是些许风霜罢了" + time++);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (time == 6){
                break;
            }
        }
    }
}
调用star方法开启支线程,如果不用star,直接调用run方法,那么相当于一个主线程的普通的方法,会让主线程按照顺序执行下去
 而开启支线程,主线程和支线程是分开独立,互不影响,当所有线程结束了,退出进程
第二种:
 
 调用方法不同。需要创建一个Thread,再把我们实现线程的类传入后,再调用Thread方法的start
public class test1 {
    public static void main(String[] args) {
        Cat cat = new Cat();
        Thread thread = new Thread(cat);
        thread.run();
    }
}
区别

 实现接口,可以实现只创建一个对象,但是创建多个多个线程来操作
 而继承类,必须把共同资源设置为静态共享,然后创建多个对象来调用star方法
3. 线程终止
通过在我们的线程里面设置一个变量来控制线程的运行,然后在其它线程或主线程来控制这个变量
public class SellTicket {
    public static void main(String[] args) throws InterruptedException {
        Win1 win = new Win1();
        Thread thread = new Thread(win);
        thread.start();
        Thread.sleep(10 * 1000);
        win.setLoop(false);
    }
}
class Win1 implements Runnable{
    private static int num1 = 200;
    private boolean loop = true;
    //设置循环的变量,在主函数控制循环
    @Override
    public void run() {
        while (loop) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("窗口" + Thread.currentThread().getName() + "卖出还剩" + --num1 + "张票");
        }
    }
    public boolean isLoop() {
        return loop;
    }
    public void setLoop(boolean loop) {
        this.loop = loop;
    }
}
4. 常用方法

 对于第八点:
 中断线程睡眠并抛出一个异常
 
 对于yield:
 只会在CPU紧张时,才回进行让步,其余不会
在线程执行时,如果其它线程.join(),那么会等其它线程执行完,再执行本线程
如下:
 先执行主线程
 当i = 3时,停止执行主线程,执行子线程thread,执行完成后,继续执行主线程
public static void main(String[] args) throws InterruptedException {
        Win1 win = new Win1();
        Thread thread = new Thread(win);
        thread.start();
            for (int i = 0; i < 5; i++) {
                System.out.println("主线程" + i);
                Thread.sleep(2000);
                if (i == 3){
                    thread.join();
                }
            }
    }
5. 用户线程和守护线程

 守护线程,在用户线程执行完之后,自己也会终止执行
 换言之,当只剩下守护线程的时候就自动停止执行了
 设置守护线程使用setDaemon
 Win1 win = new Win1();
        Thread thread = new Thread(win);
        thread.setDaemon(true);
        thread.start();
6. 线程的七大状态

 
7. 线程的同步

 
8. 互斥锁

 对于第七点:
 对于非静态的同步方法,锁可以是this,也可以是其他对象
public  void sell() {
            synchronized (this) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    System.out.println(" ");
                }
                if (num1 <= 0) {
                    loop = false;
                    return;
                }
                System.out.println(Thread.currentThread().getName() + "还剩" + num1-- + "票");
            }
        }
对于第八点:
 如果类里面已经出现了静态的同步方法,那么这个锁就作用于这个类上,此时的非静态的方法,如果要实现同步的话,需要synchronized(这个类名.class)
 
public  synchronized static void knock(){
                System.out.println("静态已经被锁了");
        }
        public  void sell() {
            synchronized (Win1.class) {
                System.out.println();
                }
        }
9. 线程死锁

 举例:
 
10.释放锁
四种情况
 

















![[SWPUCTF 2021 新生赛]include](https://img-blog.csdnimg.cn/direct/5e36a8c8426a408d97af45254267aca2.png)

