Java多线程与并发
Java多线程常考知识点
记录一下!!!
进程和线程的区别
- 进程是资源分配的最小单位,线程是CPU调度的最小单位;
- 每一个应用程序就是一个进程,独占内存空间,保留各自状态,各个进程间互不影响;
- 线程是进程的子任务,多个线程共享进程的内存空间;
- 进程的切换开销比线程大。
Java进程和线程的关系
- Java对操作系统提供的功能进行封装,包括进程和线程;
- 运行一个程序就会产生一个进程,一个进程至少包含一个线程;
- 每个进程对应一个JVM实例,多个线程共享这个JVM实例;
- Java采用单线程编程模型,程序会自动创建主线程
start()和run()的区别
start():创建一个新的子线程并启动
run():Thread的一个方法的调用,还是在主线程执行
Thread 和 Runnable的关系
Thread是一个实现了Runnable接口的类,使得run支持多线程;
由于Java语言单继承的特性,为了提高程序扩展性,推荐使用实现Runnable接口的方式来实现多线程
处理多线程的返回值
如何给run()方法传参
- 构造函数传参
- 成员变量传参
- 回调函数传参
如何处理多线程的返回值
- 主线程等待法
- Thread.join()
package com.example.thread.threaddemo;
/**
* ClassName: HandelThreadReturn
* Function: 处理线程返回值
* 1、主线程等待法
* 2、Thread.join() 阻塞当前线程以等待子线程处理完成
* 3、Callable: FutureTask OR ThreadPool
* Date: 2020/8/27 6:45 下午
*
* @author takashi
*/
public class HandelThreadReturn implements Runnable{
String value;
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
value = "test value";
}
public static void main(String[] args) throws InterruptedException {
HandelThreadReturn aReturn = new HandelThreadReturn();
Thread thread = new Thread(aReturn);
thread.start();
// 1、主线程等待法
while (aReturn.value == null) {
Thread.sleep(100);
}
// 2、阻塞当前线程以等待子线程处理完成
// Thread.join();
System.out.println("return value: "+ aReturn.value);
}
}
- 使用FutureTask
- 自定义MyCallable类:
package com.example.thread.threaddemo;
import java.util.concurrent.Callable;
/**
* ClassName: MyCallable
* Function: TODO
* Date: 2020/8/27 6:53 下午
*
* @author takashi
*/
public class MyCallable implements Callable {
@Override
public Object call() throws Exception {
String value = "test";
System.out.println("ready to work ...");
Thread.sleep(5000);
System.out.println("work done ...");
return value;
}
}
- FutureTask实现:
package com.example.thread.threaddemo;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* ClassName: FutureTaskDemo
* Function: TODO
* Date: 2020/8/27 6:55 下午
*
* @author takashi
*/
public class FutureTaskDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<String> task = new FutureTask<String>(new MyCallable());
Thread thread = new Thread(task);
thread.start();
if (!task.isDone()) {
System.out.println("please wait...");
}
System.out.println("return value: "+task.get());
}
}
- 使用线程池实现:
package com.example.thread.threaddemo;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* ClassName: ThreadPoolDemo
* Function: TODO
* Date: 2020/8/27 7:00 下午
*
* @author takashi
*/
public class ThreadPoolDemo {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newCachedThreadPool();
Future future = threadPool.submit(new MyCallable());
if (!future.isDone()) {
System.out.println("please wait...");
}
try {
System.out.println("return value:" +future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}finally {
threadPool.shutdown();
}
}
}
线程的状态
- Thread源码中的状态枚举类,总共有六种:
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}
- 解释一下:
- NEW:创建后尚未启动的线程状态,既还没有调用start();
- RUNNABLE:正在Java虚拟机中执行的线程或者正在等待CPU分配执行时间的线程状态;
- BLOCKED:等待获取排它锁的线程状态;
- WAITING:无期限等待,不会主动获取CPU时间片,需要显示唤醒,调用以下方法会进入该状态:
没有设置超时时间参数的:Thread.sleep();
没有设置超时时间参数的:Object.wait();
调用LockSupport工具类的park()方法; - TIME_WAITING:有期限等待,不会主动获取CPU时间片,不需要显示唤醒,到达设置的超时时间后,由系统自动唤醒,调用以下方法会进入该状态:
Thread.join();
设置了超时时间参数的:Thread.sleep();
设置了超时时间参数的:Object.wait();
LockSupport.parkNanos();
LockSupport.parkUntil(); - TERMINATED:线程生命周期结束
sleep和wait的区别
- 所属的类不同:sleep()属于Thread类,wait()属于Object类;
- 使用地方不同:sleep()可以在任何地方使用,wait()只能在Synchronized方法或Synchronized代码块中使用;
- 是否会释放锁:两者都会让出CPU资源,但sleep()不会释放锁,而wait()会释放锁;
notify 和 notifyall的区别
-
前置知识:
- 锁池:A、B、C三个线程去获取由一个对象所管理的锁,当线程A获取到了锁之后,B和C将会进入该对象的锁池中;
- 等待池:线程A调用了管理锁的对象的wait()方法,此时线程A会进入该对象的等待池中,线程A不会去抢占该对象所持有的锁;
-
notifyall:会让所有进入了等待池中的线程进入锁池,从而有机会能够获取到对象锁
-
notify:从等待池中随机选取一个线程,使其进入锁池中去抢占对象锁;
yield函数
- 暗示会让出CPU执行时间,但最终的决定权是由线程调度器决定的
如何中断线程
-
调用stop()方法
线程A调用线程B的stop()来停止B线程,此时线程B会马上释放锁,可能会导致数据不一致的现象发生。且由于线程A并不知道线程B执行的具体情况,可能会导致线程B的清理工作无法完成。线程该方法已经被抛弃使用
-
调用suspend()和resume()
这两种方法也已经被弃用 -
调用interrupt(),通知线程应该被中断了
如果线程处于BLOCKED状态,此时被通知的线程会立即退出BLOCKED的状态,且会抛出InterruptedException异常;如果线程处于正常活动状态,那么该线程的中断标记则被设置为true。被设置中断标记的线程继续运行,不受影响。
因此,Interrupt()只是通知被调用的线程该中断了,并不能真正的中断线程,需要被通知的线程配合才能进行线程的中断。