史上最全最简洁的Java 多线程基础,没有之一!!!
Java 多线程基础
1 线程的概念
1.1 线程是进程内的执行单元
1.2 线程基本操作
class MyThread extends Runnable{
@Override
public void run(){
}
}
MyThread t = new MyThread();
t.start();
-
Thread.run 方法的实现
-
public void run(){ //target 是Runnable接口 if(target!=null){ target.run(); } } -
new Thread(new CreateThread()).run()
-
1.3 Thread.stop 方法 --deprecated
- 释放所有的monitor 方法
- 即刻抛出ThreadDeath异常,在线程的run()方法内,任何一点都有可能抛出ThreadDeath Error,包括在catch或finally语句中。
- stop方法会抛出一个ThreadDeath异常,这时候run方法也就执行结束了,线程就终止了,这种是用抛异常来结束线程的,但是这种抛出线程是不安全的,因为他不可控制,不知道到在run方法中的何处就可能抛出异常,所以是危险的
- 会释放该线程所持有的所有的锁,而这种释放是不可控制的,非预期的。
1.4 中断线程
1.4.1 interrupt 操作
public void Thread.interrupt()//中断线程public boolean Thread.isInterrupted//判断是否中断public static boolean Thread.interrupted//判断是否中断,并清除当前的中断状态
public void run(){
while(true){
if (Thread.currentThread.isInterrupted()){
break;
}
//do something
Thread.yield();
}
}
thread.interrupt(); //中断线程
1.4.2 sleep 解析
public static native void sleep(long millis) throws InterruptedException;
public void run(){
while(true){
if (Thread.currentThread.isInterrupted()){
break;
}
try{
Thread.sleep(2000);
}catch(InterruptedException e){
//设置中断状态,抛出异常后会清除中断标记位
Thread.currentThread().interrupt();
}
//do something
Thread.yield();
}
}
1.5 挂起(suspend)继续执行(resume)
-
suspend --deprecated
-
If the target thread holds a lock on the monitor protecting a critical system resource when it is suspended, no thread can access this resource until the target thread is resumed
-
If
the thread that would resume the target thread attempts to lock this
monitor prior to callingresume, deadlock results. Such
deadlocks typically manifest themselves as "frozen" processes.
-
1.6 yield && join
-
yield
-
谦让
-
A hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore this
hint.
-
-
join
-
等待线程结束
-
while(isAlive){ wait(0); } -
Waits at most {@code millis} milliseconds for this thread to die.
A timeout of {@code 0} means to wait forever.
-
It is recommended that
applications not use {@code wait}, {@code notify}, or
{@code notifyAll} on {@code Thread} instances.
-
1.7 守护线程 daemon
-
在后台默默完成一些系统性的服务,比如垃圾回收线程,JIT 线程
-
在一个Java 应用内,只有守护线程式,Java 虚拟机会自然退出
-
Thread t = new MyThread(); t.setDaemon(true); t.start() //直接退出,开启之前设置
-
1.8 优先级线程
- MIN_PRIORITY、NORM_PRIORITY、MAX_PRIORITY
1.9 线程的同步
1.9.1 synchronized
-
指定加锁对象:给指定的对象锁,进入同步代码块之前,获得指定对象的锁
-
public class AccountingSync implements Runnable{ static AccountingSync instance = new AccountSync(); static int i=0; @Override public void run(){ for(int j=0;j<1000;j++){ synchronized(instance){ i++; } } } public static void main(String[] args){ Thread t1=new Thread(instance); Thread t2=new Thread(instance); t1.start();t2.start(); t1.join();t2.join(); System.out.println(i); //sout---> 20000 } }
-
-
直接作用于实例方法,相当于对当前实例进行加锁,
-
public class AccountingSync implements Runnable{ static AccountingSync instance = new AccountSync(); static int i=0; public static sychronized void increase1(){ i ++; //锁加在了当前对象实例上 } @Override public void run(){ for(int j=0;j<1000;j++){ increase1(); /// } } } public static void main(String[] args){ Thread t1=new Thread(instance); Thread t2=new Thread(instance); t1.start();t2.start(); t1.join();t2.join(); System.out.println(i); //sout---> 20000 } }
-
-
直接作用于 静态方法,相当于对当前类加锁
-
public sychronized void increase1(){ i ++; //锁加在了当前对象实例上 }
-
1.9.2 wait & notify()
-
wait 方法 写在 synchronize之内
This method should only be called by a thread that is the owner of this object's monitor. See the {@code notify} method for a description of the ways in which a thread can become the owner of a monitor. -
notify 调用之前获取对象的监视器
-
notifyAll

浙公网安备 33010602011771号