黑马程序员 多线程

来源:互联网 发布:linux编辑文件命令vim 编辑:程序博客网 时间:2024/06/01 11:26

------- android培训、java培训、期待与您交流! ----------

一、创建线程的方式

1、继承Thread

   步骤:继承Thread   覆盖run方法   建立线程对象  调用start方法启动

2、实现Runablei接口

  步骤:定义Runable接口    覆盖Runable接口中的run方法  通过Thread 类建立线程对象  将Runable接口的子类对象作为实际参数传递给Thread类构造函数  调用Thread类的start方法

二、线程安全问题

synchronized(对象){

需要同步的代码块

}

同步函数使用的锁是this,静态修饰后使用的锁是该类色字节码文件即类名.Class


同步函数两前提:1.至少两线程   2.统一锁

 单例设计模式

懒汉式——效率低

class Singer
{
    private static Singer s=null;
    private Singer(){};
    private static Singer getInstance
    {
            if(s==null){
                synchronized(Singer.Class)
                {
                    if(s==null)
                    s=new Singer();
                }
            }
        }
        return s;    
}

通过双if来提高效率

死锁:

package com.heima.demo;

public class DeadLock {

    /**
     * @param args
     */

    public static void main(String[] args) {
        
        Thread t1 = new Thread(new Dead(true));
        Thread t2 = new Thread(new Dead(false));
        t1.start();
        t2.start();

    }
}

class Lock {
    static Object A = new Object();
    static Object B = new Object();
}

class Dead implements Runnable {
    private boolean falg;

    Dead(boolean falg) {
        this.falg = falg;
    }

    public void run() {
        if (falg) {
            synchronized (Lock.A) {
                System.out.println("if Lock A");
                synchronized (Lock.B) {
                    System.out.println("if Lock B");
                }
            }
        } else {
            synchronized (Lock.B) {
                System.out.println("else B");
                synchronized (Lock.A) {
                    System.out.println("else A");
                }
            }
        }

    }

}
三、线程间通信-等待唤醒机制

通过当前的锁来调用wait()、 notify()方法


package com.itheima;

public class Test10 {

    /**
     * @param args
     *            10、 模拟妈妈做饭,做饭时发现没有盐了,让儿子去买盐(假设买盐需要3分钟), 只有盐买回来之后,妈妈才能继续做饭的过程
     *
     */
    public static void main(String[] args) {

        Salt salt = new Salt();
        Mother m = new Mother(salt);
        Son s = new Son(salt);
        Thread t1 = new Thread(m);
        Thread t2 = new Thread(s);
        t1.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        t2.start();

    }

}

class Salt {

    boolean falg = true;

}

class Mother implements Runnable {

    private Salt salt;

    Mother(Salt salt) {
        this.salt = salt;
    }

    public void run() {
        synchronized (salt) {
            System.out.println("Mother Cooking");

            if (salt.falg) {

                System.out.println("Son Buy Salt");
                try {
                    salt.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

            System.out.println("Mother Cook");
            salt.notify();
        }

    }

}

class Son implements Runnable {

    private Salt salt;

    Son(Salt salt) {
        this.salt = salt;
    }

    public void run() {
        synchronized (salt) {
            if (!salt.falg) {
                try {
                    salt.wait();
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                System.out.println("Buy Salt");
                try {
                    Thread.sleep(3 * 60000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            salt.falg = true;
            salt.notifyAll();
        }
    }

}

 生产者 消费者

while循环判断标记

notifyAll()方法唤醒所有进程

新特性  Lock Conition


停止线程

让run()方法结束即可停止线程

 用interrupt()方法中断阻塞状态


join加入线程,其他线程等待

原创粉丝点击