java学习-day5
多线程学习
一、线程状态 五大状态

停止线程:推荐使用标识位进行终止package com.Threadpublic class TestStop implements Runnable private boolean flag = true;
@Override
public void run() {
int i = 0;
while(flag){
System.out.println();
}
}
public void stop(){
this.flag = false;
}
public static void main(String[] args) {
TestStop testStop = new TestStop();
new Thread(testStop).start();
for (int i = 0; i < 1000; i++) {
System.out.println("main"+i);
if(i == 900){
testStop.stop();
System.out.println("线程停止");
}
}
}
}
线程休眠
sleep制定当前线程阻塞的毫秒数;
sleep存在异常InterruptedException;
sleep时间达到后线程进入就绪状态;
sleep可以模拟网络延时,倒计时等;
每一个对象都有一个锁,sleep不会释放锁。
线程礼让
礼让:让当前正在执行的线程暂停,但不阻塞;
礼让不一定成功;
从执行状态转换为就绪状态;
package com.Thread;
public class Testyeild {
public static void main(String[] args) {
MyYeild myYeild = new MyYeild();
new Thread(myYeild,"a").start();
new Thread(myYeild,"b").start();
}
}
class MyYeild implements Runnable{
@Override
public void run(){
System.out.println(Thread.currentThread().getName()+"线程开始");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"线程结束");
}
}
Join合并线程,强制执行package com.Thread;
public class TestJoin implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("线程vip");
}
}
public static void main(String[] args) throws InterruptedException {
TestJoin testJoin = new TestJoin();
Thread thread = new Thread(testJoin);
for (int i = 0; i < 1000; i++) {
if (i == 200){
thread.start();
thread.join();
}
System.out.println("main"+i);
}
}
}
二、线程观测
package com.Thread;
public class TestState {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("/////");
});
Thread.State state = thread.getState();
System.out.println(state);//new
thread.start();
state = thread.getState();
System.out.println(state);//runnable
while (state != Thread.State.TERMINATED){
Thread.sleep(100);
state = thread.getState();
System.out.println(state);
}
}
}
三、线程优先级 线程调度器按照优先级决定应该调度哪个线程来执行。 PRIORITY 从1-10;、
getPriority().setPriority(int ***);
四、守护线程 线程分为用户线程和守护线程
虚拟机必须确保用户线程执行完毕;
虚拟机不用等待守护线程执行完毕;
五、线程同步 多个线程操作同一个资源; 其实是一种等待机制 队列和锁 synchronized
同步方法:public synchronized void method(int args){}
同步块:synchronized(obj):锁的对象就是变化的量
六、死锁 某一个同步块同时拥有“两个以上对象的锁”时,就可能会发生死锁的问题;
四个必要条件:互斥,一个资源每次只能被一个进程使用;
请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放;
不剥夺资源:进程已获得的资源,在未使用完之前,不能强行剥夺;
循环等待条件,若干进程之间形成一种头尾衔接的循环等待资源关系;
解决:破坏上述四个条件之一就可以避免死锁发生;
七、Lock锁 通过显示定义同步锁;同步锁使用LOCK对象充当; ReentranLock类实现了Lock
优点:JVM花费较少的时间来调度线程,性能更好,并且具有更好的扩展性;
package com.Thread;
import java.util.concurrent.locks.ReentrantLock;
public class TestLock {
public static void main(String[] args) {
TestLock2 testLock2 = new TestLock2();
new Thread(testLock2).start();
new Thread(testLock2).start();
new Thread(testLock2).start();
}
}
class TestLock2 implements Runnable{
int ticketNum = 10;
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
while (true) {
try {
lock.lock();
if (ticketNum > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(ticketNum--);
} else {
break;
}
} finally {
lock.unlock();
}
}
}
}
八、线程池
好处: 提高响应速度 降低资源消耗 便于线程管理
corePoolSize:核心池大小
maximumPoolSize:最大线程数
keepAliveTime:线程没有任务时最多保持多久时间后会终止;
exEcutorService 接口
浙公网安备 33010602011771号