【216天】黑马程序员27天视频学习笔记【Day24-下】

叨逼叨两句

人工智能只会是打工获取高工资的机会,区块链技术才是未来我的创业机会。

24-15:设置线程的优先级

  1. setPriority()
  2. 线程优先级设置范围为1-10
package com.test.demo001;

public class Demo015{
    public static void main(String[] args) {
        Thread t1 = new Thread(){
            public void run(){
                for(int i = 0; i < 100; i++){
                    System.out.println(".....aaaaa");
                }
            }
        };
        
        Thread t2 = new Thread(){
            public void run(){
                for(int i = 0; i < 100; i++){
                    System.out.println("....bbb");
                }
            }
        };
        //t1.setPriority(10);
        //t2.setPriority(1);
        
        t1.setPriority(Thread.MAX_PRIORITY);//
        t2.setPriority(Thread.MIN_PRIORITY);
        
        t1.start();
        t2.start();
    }
}

24-16:同步代码块

  1. 应用场景

    • 当多线程并发,有多段代码同时执行时,我们希望某一段代码执行的过程中CPU不要切换到其它线程工作,这时就需要同步。
    • 如果两段代码是同步的,那么同一时间只能执行一段,在一段代码没有执行结束之前,不会执行另外一段。
  2. 同步代码块

    • 使用synchronized关键字加上一个锁对象来定义一段代码,这就叫同步代码块。
    • 多个同步代码块如果使用相同的锁对象,那么他们就是同步的
  3. 不使用锁:
package com.test.demo001;

public class Demo015{
    public static void main(String[] args) {
        Thread t1 = new Thread(){
            public void run(){
                for(int i = 0; i < 100; i++){
                    System.out.println("你");
                    System.out.println("好");
                    System.out.println("世");
                    System.out.println("界");
                }
            }
        };
        
        Thread t2 = new Thread(){
            public void run(){
                for(int i = 0; i < 100; i++){
                    System.out.println("Hello");
                    System.out.println("!");
                    System.out.println("My");
                    System.out.println("World");
                }
            }
        };
        t1.start();
        t2.start();
    }
}

  1. 使用锁

package com.test.demo001;

public class Demo015{
    public static void main(String[] args) {
        Demo d = new Demo();
        Thread t1 = new Thread(){
            public void run(){
                synchronized(d){
                    for(int i = 0; i < 100; i++){
                        System.out.println("你");
                        System.out.println("好");
                        System.out.println("世");
                        System.out.println("界");
                    }
                };
            }
        };
        
        Thread t2 = new Thread(){
            public void run(){
                synchronized(d){
                    for(int i = 0; i < 100; i++){
                        System.out.println("Hello");
                        System.out.println("!");
                        System.out.println("My");
                        System.out.println("World");
                    }
                };
            }
        };
        t1.start();
        t2.start();
    }
}

class Demo {
    
}

  1. synchronize(d)中这个参数d可以是任意对象

24-17:同步方法

  1. 非静态的同步方法的锁对象是this
package com.test.demo001;

public class Demo015{
    public static void main(String[] args) {
        Demo d = new Demo();
        Thread t1 = new Thread(){
            public void run(){
                d.print1();    
            }
        };
        
        Thread t2 = new Thread(){
            public void run(){
                d.print2();
            }
        };
        t1.start();
        t2.start();
    }
}

class Demo {
    public synchronized void print1(){
        for(int i = 0; i < 100; i++){
            System.out.println("你");
            System.out.println("好");
            System.out.println("世");
            System.out.println("界");
        };
    }
    
    public void print2(){
        synchronized(this){
            for(int i = 0; i < 100; i++){
                System.out.println("Hello");
                System.out.println("!");
                System.out.println("My");
                System.out.println("World");
            }
        }
    }
}
  1. 静态的同步方法锁对象是:

    1. 字节码对象
    2. 外面声明的静态对象
package com.test.demo001;

public class Demo015{
    public static void main(String[] args) {
        Demo d = new Demo();
        Thread t1 = new Thread(){
            public void run(){
                d.print1();    
            }
        };
        
        Thread t2 = new Thread(){
            public void run(){
                d.print2();
            }
        };
        t1.start();
        t2.start();
    }
}

class Demo {
    public static synchronized void print1(){
        for(int i = 0; i < 100; i++){
            System.out.println("你");
            System.out.println("好");
            System.out.println("世");
            System.out.println("界");
        };
    }
    
    public void print2(){
        synchronized(Demo.class){
            for(int i = 0; i < 100; i++){
                System.out.println("Hello");
                System.out.println("!");
                System.out.println("My");
                System.out.println("World");
            }
        }
    }
}

24-(18-19):线程安全问题【重要!这个视频再看一遍】

24-18:火车站卖票例子的继承Thread类实现

  1. 多线程并发操作同一数据时,就有可能出现线程安全问题
  2. 使用同步技术可以解决这种问题,把操作数据的代码进行同步,不要多个线程一起操作
package com.test.demo001;

public class Demo015{
    public static void main(String[] args) {
        new Ticket().start();
        new Ticket().start();
        new Ticket().start();
        new Ticket().start();
    }
}

class Ticket extends Thread{
    private static int ticket = 100;
    public void run(){
        while(true){
            synchronized(Ticket.class){
                if(ticket == 0){
                    break;
                }
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(getName()+ "这是第"+ticket--+"张");
            }
        }
    }
}

24-19:火车站卖票例子的实现Runnable接口实现

package com.test.demo001;

public class Demo015{
    public static void main(String[] args) {
        Ticket t = new Ticket();
        new Thread(t).start(); 
        new Thread(t).start(); 
        new Thread(t).start(); 
        new Thread(t).start();         
    }
}

class Ticket implements Runnable{
    private int ticket = 100;
    public void run(){
        while(true){
            synchronized(this){
                if(ticket == 0){
                    break;
                }
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+ "这是第"+ticket--+"张");
            }
        }
    }
}

24-20:死锁

  1. 多线程同步时,同步代码不要嵌套,大概率会死锁
package com.test.demo001;

public class Demo015{
    public static void main(String[] args){
        new Thread(){
            public void run(){
                while(true){
                    synchronized(s1){
                        System.out.println(getName()+"获取" + s1 + "等待" + s2);
                        synchronized(s2){
                            System.out.println(getName()+"拿到" + s2 + "开吃");
                        }
                    }
                }
            }
        }.start();
        
        new Thread(){
            public void run(){
                while(true){
                    synchronized(s2){
                        System.out.println(getName()+"获取" + s2 + "等待" + s1);
                        synchronized(s1){
                            System.out.println(getName()+"拿到" + s1 + "开吃");
                        }
                    }
                }
            }
        }.start();
    }
    
    private static String s1 = "筷子左";
    private static String s2 = "筷子右";
    
    

}

24-21:线程安全的类的回顾

  1. Vector是线程安全的,ArrayList是线程不安全的
  2. StringBuffer是线程安全的,StringBuilder是线程不安全的
  3. Hashtable是线程安全的,HashMap是线程不安全的
  4. Collections.synchronized(XXX)可以将线程不安全的变成线程安全的

评论

暂无评论