java多线程学习

1、理解线程、单线程、多线程、进程

  • 线程:进程的执行单元,执行路径
  • 单线程:一个应用程序只有一条执行路径
  • 多线程:一个应用程序有多条执行路径
  • 进程:正在执行的应用程序

2、多线程的意义?

  • 多线程的存在,不是提高程序的执行速度。其实是为了提高应用程序的使用率

3、多线程的实现方法

  • 继承Thread类  
  1. public class MyThread extends Thread {  
  2.     @Override  
  3.     public void run() {  
  4.         for (int i = 0; i <= 100; i++) {  
  5.                 System.out.println(Thread.currentThread().getName()+i );  
  6.         }  
  7.     }     
  8.     public static void main(String[] args) {  
  9.         MyThread thread1 = new MyThread();  
  10.         MyThread thread2 = new MyThread();  
  11.         MyThread thread3 = new MyThread();  
  12.         thread1.start();  
  13.         thread2.start();  
  14.         thread3.start();   
  15.     }  
  16. }  
  • 实现Runnable接口
  1. public class MyThreadRunnable implements Runnable {  
  2.     @Override  
  3.     public void run() {  
  4.         for (int i = 0; i <= 100; i++) {  
  5.                 System.out.println(Thread.currentThread().getName()+i);  
  6.         }  
  7.     }  
  8.  
  9.     public static void main(String[] args) {  
  10.         MyThreadRunnable myRunnable = new MyThreadRunnable();  
  11.         Thread thread1 = new Thread(myRunnable, "MyThreadRunnable1");  
  12.         Thread thread2 = new Thread(myRunnable, "MyThreadRunnable2");  
  13.         Thread thread3 = new Thread(myRunnable, "MyThreadRunnable3");  
  14.  
  15.         thread1.start();  
  16.         thread2.start();  
  17.         thread3.start();  
  18.     }  

4、Runnable和Thread区别

  • 一个类可以继承多个接口,Thread是类,Runnable是接口,所有相对于好点
  • start()方法在Runnable中没有
  • Thread类中,有一个构造方法:public Thread(Runnable targer)

5、线程的调度和优先级问题

  • 调度  
  1. 抢cpu调度的方式  
  2. 分时调度  
  • 优先级    setPriority(int newPriority) 
  1. 默认是5  
  2. 范围1-10

6、线程的常见方法

  • 开启线程
start()

导致此线程开始执行; Java虚拟机调用此线程的run方法。

  • 休眠线程  
sleep(long millis)

使当前正在执行的线程以指定的毫秒数暂停(暂时停止执行),具体取决于系统定时器和调度程序的精度和准确性。

 
sleep(long millis, int nanos)

导致正在执行的线程以指定的毫秒数加上指定的纳秒数来暂停(临时停止执行),这取决于系统定时器和调度器的精度和准确性。

  •  中断线程
interrupt()

中断这个线程。

  •  暂停线程
yield()

对调度程序的一个暗示,即当前线程愿意产生当前使用的处理器。

  • 加入线程
join()

等待这个线程死亡。

 7、线程的生命周期

  1. 新建  创建对象  对象调用start()进入就绪状态。。
  2. 就绪   有执行资格,没有执行的权利,获取到cpu的执行权进入运行状态。。
  3. 运行  线程run运行结束 或者线程中断 结束线程 到死亡阶段。。运行过程中cpu的执行权被别的程序抢到,则进入就绪状态
  4. 阻塞  阻塞状态cpu没有执行权。。在运行过程中线程进行等待(wait)或者休眠(sleep)到阻塞状态,休眠时间到,或者调用notify则进入就绪状态
  5. 死亡  对象变成垃圾进行回收

8、多线程安全问题

  1. SQL多条操作问题
  2. 共享数据问题
  3. 多线程环境问题

9、同步解决线程安全问题

  1. 同步方法
  2. 静态同步方法
  3. 同步代码块
  • 卖票案例 ---同步代码块

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();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值