/**
* 线程常用方法
*/
public class ThreadMethod {
public static void main(String[] args) throws InterruptedException {
TextThread textThread = new TextThread();
// 设置线程名称
textThread.setName("dahe");
// 设置线程优先级
textThread.setPriority(Thread.MIN_PRIORITY);
// 启动子线程
textThread.start();
// 获取线程优先级
System.out.println(textThread.getPriority());
// 中断线程休眠
// textThread.interrupt();
}
}
class TextThread extends Thread {
@Override
public void run() {
// 线程休眠
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// 获取当前线程的名称
System.out.println(Thread.currentThread().getName());
}
}
------------------------------------
输出:
1
dahe
/**
* 线程常用方法
*/
public class ThreadMethod {
public static void main(String[] args) throws InterruptedException {
TextThread textThread = new TextThread();
// 设置线程名称
textThread.setName("dahe");
// 设置线程优先级
textThread.setPriority(Thread.MIN_PRIORITY);
// 启动子线程
textThread.start();
// 获取线程优先级
System.out.println(textThread.getPriority());
// 中断线程休眠
// textThread.interrupt();
DDD ddd = new DDD();
ddd.start();
for (int i = 0; i < 10; i++) {
// 线程礼让,让出CPU,但礼让的时间不确定,所以也不一定礼让成功
if (i == 2) {
Thread.yield();
}
// 让子线程插队,ddd先执行完再继续执行子线程
if (i == 5) {
ddd.join();
}
Thread.sleep(1000);
System.out.println("主线程吃包子" + i);
}
}
}
class TextThread extends Thread {
@Override
public void run() {
// 线程休眠
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// 获取当前线程的名称
System.out.println(Thread.currentThread().getName());
}
}
class DDD extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("我是DDD!我在吃包子" + i);
}
}
}
线程分为用户线程和守护线程
常见的守护线程:垃圾回收机制🐭
程序示例:
/**
* 守护线程
*/
public class DaemonThread {
public static void main(String[] args) throws InterruptedException {
AAA aaa = new AAA();
// 希望主线程结束后,子线程可以自动结束,将子线程设置为守护线程即可
aaa.setDaemon(true);
aaa.start();
for (int i = 0; i < 10; i++) {
Thread.sleep(300);
System.out.println("主线程");
}
}
}
class AAA extends Thread {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("我是AAA!我在吃包子" + i);
}
}
}
输出分析:
主线程
我是AAA!我在吃包子0
主线程
我是AAA!我在吃包子1
主线程
我是AAA!我在吃包子2
主线程
我是AAA!我在吃包子3
主线程
我是AAA!我在吃包子4
主线程
我是AAA!我在吃包子5
主线程
我是AAA!我在吃包子6
主线程
我是AAA!我在吃包子7
主线程
我是AAA!我在吃包子8
主线程
虽然子线程存在1000此的循环输出,但是其为守护线程,主线程退出后,其自动销毁😶🌫️
线程是一个动态执行的过程,它也有一个从产生到死亡的过程。
下图显示了一个线程完整的生命周期。

使用了synchronized标识之后,所标识的内容就会加入互斥锁🔒在任意时刻,只能有一个线程访问该对象
注意:这会导致程序的执行效率降低😈
例如:(在方法上面加锁🔒)
class SellTicketRes implements Runnable {
private int ticketNum = 100;
private boolean loop = true;
/**
* 使用synchronized实现线程同步,解决超卖
* 这时的锁在this对象
*/
public synchronized void Sell() {
if (ticketNum <= 0) {
System.out.println("没有余票!");
loop = false;
return;
}
// 休眠卖票
try {
Thread.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("窗口" + Thread.currentThread().getName() +
"售出一张票。" + "剩余票数:" + (--ticketNum));
}
@Override
public void run() {
while (true) {
if (loop == false) {
break;
}
Sell();
}
}
}
为了增大效率,也可以不在方法中同步,转而在代码块中进行同步加锁:
注意,这时的锁依然是在this对象
class SellTicketRes implements Runnable {
private int ticketNum = 100;
private boolean loop = true;
/**
* 使用synchronized实现线程同步,解决超卖
* 这时的锁在this对象
*/
public void Sell() {
synchronized (this) {
if (ticketNum <= 0) {
System.out.println("没有余票!");
loop = false;
return;
}
// 休眠卖票
try {
Thread.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("窗口" + Thread.currentThread().getName() +
"售出一张票。" + "剩余票数:" + (--ticketNum));
}
}
@Override
public void run() {
while (true) {
if (loop == false) {
break;
}
Sell();
}
}
}
同步静态方法的锁,是加在当前类本身,例如:ThreadLock.class
// 同步静态方法的锁,是加在当前类本身ThreadLock.class
public synchronized static void mm(){
}
所以在静态方法的内部的代码块枷锁,需要传入当前的类信息:
public static void yy() {
synchronized (ThreadLock.class) {
System.out.println("猪猪!");
}
}
多个线程都占用了对方的锁资源,但不肯相让,导致了死锁🔒,死锁🔒是多线程开发的大忌!❌
为了方便理解,我们来看下面一段程序:
/**
* 线程死锁
*/
class DeadLock extends Thread {
static Object o1 = new Object();
static Object o2 = new Object();
boolean flag;
public DeadLock(boolean flag) {
this.flag = flag;
}
@Override
public void run() {
if (flag) {
synchronized (o1) {
System.out.println(Thread.currentThread().getName() + "进入1");
synchronized (o2) {
System.out.println(Thread.currentThread().getName() + "进入2");
}
}
} else {
synchronized (o2) {
System.out.println(Thread.currentThread().getName() + "进入3");
synchronized (o1) {
System.out.println(Thread.currentThread().getName() + "进入4");
}
}
}
}
}
分析:
最终两个线程并发执行的时候,会发生死锁,输出以下并且卡住❌:
Thread-0进入1
Thread-1进入3
释放锁的情况:
break,returnError和Exceptionwait方法,当前线程会暂停并暂时释放锁🔒注意,以下的操作并不会释放锁:
Thread.sleep()方法,Thread.yield()方法暂停当前线程的执行,并不会释放锁suspend()方法将该线程挂起,不会释放锁