1、理解线程、单线程、多线程、进程
- 线程:进程的执行单元,执行路径
- 单线程:一个应用程序只有一条执行路径
- 多线程:一个应用程序有多条执行路径
- 进程:正在执行的应用程序
2、多线程的意义?
- 多线程的存在,不是提高程序的执行速度。其实是为了提高应用程序的使用率
3、多线程的实现方法
- 继承Thread类
- public class MyThread extends Thread {
- @Override
- public void run() {
- for (int i = 0; i <= 100; i++) {
- System.out.println(Thread.currentThread().getName()+i );
- }
- }
- public static void main(String[] args) {
- MyThread thread1 = new MyThread();
- MyThread thread2 = new MyThread();
- MyThread thread3 = new MyThread();
- thread1.start();
- thread2.start();
- thread3.start();
- }
- }
- 实现Runnable接口
- public class MyThreadRunnable implements Runnable {
- @Override
- public void run() {
- for (int i = 0; i <= 100; i++) {
- System.out.println(Thread.currentThread().getName()+i);
- }
- }
- public static void main(String[] args) {
- MyThreadRunnable myRunnable = new MyThreadRunnable();
- Thread thread1 = new Thread(myRunnable, "MyThreadRunnable1");
- Thread thread2 = new Thread(myRunnable, "MyThreadRunnable2");
- Thread thread3 = new Thread(myRunnable, "MyThreadRunnable3");
- thread1.start();
- thread2.start();
- thread3.start();
- }
- }
4、Runnable和Thread区别
- 一个类可以继承多个接口,Thread是类,Runnable是接口,所有相对于好点
- start()方法在Runnable中没有
- Thread类中,有一个构造方法:public Thread(Runnable targer)
5、线程的调度和优先级问题
- 调度
- 抢cpu调度的方式
- 分时调度
- 优先级 setPriority(int newPriority)
- 默认是5
- 范围1-10
6、线程的常见方法
- 开启线程
start() 导致此线程开始执行; Java虚拟机调用此线程的 |
- 休眠线程
sleep(long millis) 使当前正在执行的线程以指定的毫秒数暂停(暂时停止执行),具体取决于系统定时器和调度程序的精度和准确性。 |
sleep(long millis, int nanos) 导致正在执行的线程以指定的毫秒数加上指定的纳秒数来暂停(临时停止执行),这取决于系统定时器和调度器的精度和准确性。 |
- 中断线程
interrupt() 中断这个线程。 |
- 暂停线程
yield() 对调度程序的一个暗示,即当前线程愿意产生当前使用的处理器。 |
- 加入线程
join() 等待这个线程死亡。 |
7、线程的生命周期
- 新建 创建对象 对象调用start()进入就绪状态。。
- 就绪 有执行资格,没有执行的权利,获取到cpu的执行权进入运行状态。。
- 运行 线程run运行结束 或者线程中断 结束线程 到死亡阶段。。运行过程中cpu的执行权被别的程序抢到,则进入就绪状态
- 阻塞 阻塞状态cpu没有执行权。。在运行过程中线程进行等待(wait)或者休眠(sleep)到阻塞状态,休眠时间到,或者调用notify则进入就绪状态
- 死亡 对象变成垃圾进行回收
8、多线程安全问题
- SQL多条操作问题
- 共享数据问题
- 多线程环境问题
9、同步解决线程安全问题
- 同步方法
- 静态同步方法
- 同步代码块
- 卖票案例 ---同步代码块
public class SellTicket implements Runnable {
// 定义100张票
private int tickets = 100;
// 定义同一把锁 锁可以是任意对象
private Object obj = new Object();
@Override
public void run() {
while (true) {
// t1,t2,t3都能走到这里
// 假设t1抢到CPU的执行权,t1就要进来
// 假设t2抢到CPU的执行权,t2就要进来,发现门是关着的,进不去。所以就等着。
// 门(开,关)
synchronized (obj) { // 发现这里的代码将来是会被锁上的,所以t1进来后,就锁了。(关)
if (tickets > 0) {
try {
Thread.sleep(100); // t1就睡眠了
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()
+ "正在出售第" + (tickets--) + "张票 ");
//窗口1正在出售第100张票
}
} //t1就出来可,然后就开门。(开)
}
}
public static void main(String[] args) {
// 创建资源对象
SellTicket st = new SellTicket();
// 创建三个线程对象
Thread t1 = new Thread(st, "窗口1");
Thread t2 = new Thread(st, "窗口2");
Thread t3 = new Thread(st, "窗口3");
// 启动线程
t1.start();
t2.start();
t3.start();
}
}