1 线程的概念
1.1 线程与进程
这部分内容其实是属于《操作系统》这门课程的,接下来将详细的请看大佬的博客 操作系统常见面试题总结(上)
守护进程:备胎进程,就是当非守护进程执行完毕之后,守护进程会逐渐关闭,即还没有将任务完全执行完毕,也会提前关闭,当时守护进程不会立即关闭。
1.2 线程的实现
1.3 线程池
核心线程:线程池中最大线程数 队伍长度:排队等待的线程数量 临时线程:队伍排满后,才会创建临时线程
2 代码实践
2.1 继承 Thread 类
package cn. edu. njust. threaddemo ;
public class MyThread extends Thread {
@Override
public void run ( ) {
super . run ( ) ;
for ( int i = 0 ; i < 10 ; i++ ) {
System . out. println ( getName ( ) + ": 测试" + i) ;
}
}
}
package cn. edu. njust. threaddemo ;
public class ThreadDemo01 {
public static void main ( String [ ] args) {
MyThread mt1 = new MyThread ( ) ;
mt1. setName ( "线程1" ) ;
mt1. start ( ) ;
MyThread mt2 = new MyThread ( ) ;
mt2. setName ( "线程2" ) ;
mt2. start ( ) ;
}
}
2.2 实现 Runnable 接口
package cn. edu. njust. threaddemo ;
public class MyRunnable implements Runnable {
@Override
public void run ( ) {
for ( int i = 0 ; i < 10 ; i++ ) {
System . out. println ( Thread . currentThread ( ) . getName ( ) + ":测试" + i) ;
}
}
}
package cn. edu. njust. threaddemo ;
public class RunnableDemo01 {
public static void main ( String [ ] args) {
MyRunnable mr = new MyRunnable ( ) ;
Thread t1 = new Thread ( mr) ;
Thread t2 = new Thread ( mr) ;
t1. setName ( "线程1" ) ;
t2. setName ( "线程2" ) ;
t1. start ( ) ;
t2. start ( ) ;
}
}
2.3 实现 Callable 接口
package cn. edu. njust. threaddemo ;
import java. util. concurrent. Callable ;
public class MyCallable implements Callable {
@Override
public Object call ( ) throws Exception {
int res = 0 ;
for ( int i = 0 ; i < 10 ; i++ ) {
res += i;
}
return res;
}
}
package cn. edu. njust. threaddemo ;
import java. util. concurrent. ExecutionException ;
import java. util. concurrent. FutureTask ;
public class CallableDemo01 {
public static void main ( String [ ] args) throws ExecutionException , InterruptedException {
MyCallable mc1 = new MyCallable ( ) ;
FutureTask < Integer > futureTask = new FutureTask < > ( mc1) ;
Thread t1 = new Thread ( futureTask) ;
t1. start ( ) ;
Integer ans = futureTask. get ( ) ;
System . out. println ( "线程运行结果:" + ans) ;
}
}
2.4 线程安全
2.4.1 同步代码块
package cn. edu. njust. threadsafe ;
public class MyThread01 extends Thread {
private static int tickets = 0 ;
private static final Object obj = new Object ( ) ;
@Override
public void run ( ) {
while ( true ) {
synchronized ( Thread . class ) {
if ( tickets >= 100 ) break ;
try {
Thread . sleep ( 10 ) ;
} catch ( InterruptedException e) {
throw new RuntimeException ( e) ;
}
tickets ++ ;
System . out. println ( getName ( ) + "已购买第" + tickets + "张票" ) ;
}
}
}
}
package cn. edu. njust. threadsafe ;
public class ThreadTestDemo01 {
public static void main ( String [ ] args) {
MyThread01 mt1 = new MyThread01 ( ) ;
MyThread01 mt2 = new MyThread01 ( ) ;
MyThread01 mt3 = new MyThread01 ( ) ;
mt1. setName ( "客户1" ) ;
mt2. setName ( "客户2" ) ;
mt3. setName ( "客户3" ) ;
mt1. start ( ) ;
mt2. start ( ) ;
mt3. start ( ) ;
}
}
2.4.2 同步方法
package cn. edu. njust. threadsafe ;
public class MyRunnable implements Runnable {
private int tickets = 0 ;
@Override
public void run ( ) {
while ( true ) {
if ( ! buyTickets ( ) ) break ;
}
}
private synchronized boolean buyTickets ( ) {
if ( tickets >= 100 ) {
return false ;
}
try {
Thread . sleep ( 10 ) ;
} catch ( InterruptedException e) {
throw new RuntimeException ( e) ;
}
tickets++ ;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "购买了第" + tickets + "张票" ) ;
return true ;
}
}
package cn. edu. njust. threadsafe ;
public class ThreadTestDemo02 {
public static void main ( String [ ] args) {
MyRunnable mr = new MyRunnable ( ) ;
Thread t1 = new Thread ( mr, "客户1" ) ;
Thread t2 = new Thread ( mr, "客户2" ) ;
Thread t3 = new Thread ( mr, "客户3" ) ;
t1. start ( ) ;
t2. start ( ) ;
t3. start ( ) ;
}
}
2.5 线程池
2.5.1 静态方法创建
package cn. edu. njust. threaddemo ;
import java. util. concurrent. ExecutorService ;
import java. util. concurrent. Executors ;
public class ThreadPoolDemo01 {
public static void main ( String [ ] args) {
ExecutorService pool1 = Executors . newCachedThreadPool ( ) ;
ExecutorService pool2 = Executors . newFixedThreadPool ( 3 ) ;
pool2. submit ( new MyRunnable ( ) ) ;
pool2. submit ( new MyRunnable ( ) ) ;
pool2. submit ( new MyRunnable ( ) ) ;
pool2. submit ( new MyRunnable ( ) ) ;
pool2. submit ( new MyRunnable ( ) ) ;
pool1. shutdown ( ) ;
pool2. shutdown ( ) ;
}
}
2.5.2 非静态方法
3 常用方法汇总
3.1 构造器
** 方法原型** 说明 public Thread() 无参构造 public Thread(String name) 指定名字 public Thread(Runnable target) 指定任务 public Thread(Runnable target, String name) 可以在同时设置任务和设置名字
3.2 常用方法
** 方法原型** 说明 public final synchronized void setName(String name) 为线程设置名字 public final String getName() 获取线程的名字 public static native Thread currentThread() 获取到当前正在执行的线程 public static native void sleep(long millis) throws InterruptedException 使当前进程休眠一定的时间,时间结束后自动苏醒 public final void setPriority(int newPriority) 设置优先级 public final int getPriority() 或许线程的优先级 public final void setDaemon(boolean on) 将一个线程设置为守护进程 public static native void yield() 出让线程 public final void join() throws InterruptedException 将一个线程插入到当前线程之前,插入的线程执行完毕之后再执行当前线程
3.3 线程池相关方法
**方法原型 ** 说明 public static ExecutorService newCachedThreadPool() 创建一个上限是 int 类型最大值的线程池 public static ExecutorService newFixedThreadPool(int nThreads) 创建指定容量的线程池