您的位置:首页 > 大数据 > 人工智能

线程的笔记1 wait yield join interrupt deamon 死锁使用方法

2015-05-02 15:31 309 查看
package 线程;

import java.util.Date;

public class TestThread {

    /**

     * Thread 和 runnable区别: Thread 中继承该类的子类只能启动一个 start方法,开启一个线程 ,而实现

     * runnable接口的子类可以开启多个线程,访问同一个类的资源

     */

    public static void main(String[] args) {

        Thread1 ts = new Thread1();

        ts.start();

        Thread2 ts2=new Thread2();

        new Thread(ts2).start();

        new Thread(ts2).start();

    }

}

//Thread.yield();//暂停当前正在执行的线程对象,并执行其他线程。  每次执行都是短暂的暂停 然后之后自动恢复

class Thread1 extends Thread   {

    @Override

    public void run() {

        for (int i = 0; i < 100; i++) {

            System.out.println(Thread.currentThread().toString()+"----"+i);

            Thread.yield();//暂停当前正在执行的线程对象,并执行其他线程。

        }

    }

}

class Thread2 implements Runnable {

    @Override

    public void run() {

        for (int i = 0; i < 100; i++) {

            System.out.println(Thread.currentThread().toString()+"--"+i);

            //Thread.yield();//暂停当前正在执行的线程对象,并执行其他线程。

        }

    }

}

class YieldDemo extends Thread {

    public YieldDemo(String name) {

        super(name);

    }

    public void run() {

        for (int i = 0; i < 1000; i++) {

            System.out.println(this.getName() + ":" + i);

            if (i % 10 == 0) {

                this.yield();

            }

        }

    }

}

/**

 * 测试 interruput interruput 是把冻结状态中(sleep,wait,join )的线程唤醒

 *

 *

 */

class InterruptDemo implements Runnable {

    boolean flag = true;// 线程停止的 标记

    public synchronized void run() {

        while (flag) {

            System.out.println(Thread.currentThread().getName() + "=="

                    + new Date() + "==");

            try {

                System.out.println(Thread.currentThread().getName() + "wait");

                this.wait();

                // Thread.sleep(10);

            } catch (InterruptedException e) {

                e.printStackTrace();

                flag = false; // 线程被异常中断时就停止线程

            }

        }

    }

    public static void main(String[] args) {

        InterruptDemo t = new InterruptDemo();

        Thread t1 = new Thread(t);

        t1.start();

        Thread t2 = new Thread(t);

        t2.start();

        try {

            Thread.sleep(10000);

        } catch (Exception e) {

        }

        t1.interrupt();

        t2.interrupt();

    }

}

/**

 * 守护线程(后台线程) ,需要在执行start 方法前调用 与普通线程的区别是 :当正在运行的线程都是守护线程时,Java 虚拟机退出(系统结束)。 本例中

 * 两个线程都是 守护线程 当主线程结束时两个守护线程同时结束

 *

 */

class Daemon implements Runnable {

    boolean flag = true;// 线程停止的 标记

    public void run() {

        while (flag) {

            System.out.println(Thread.currentThread().getName() + "=="

                    + new Date() + "==");

            

        }

    }

    public static void main(String[] args) {

        Daemon t = new Daemon();

        Thread t1 = new Thread(t);

        t1.setDaemon(true);

        t1.start();

        Thread t2 = new Thread(t);

        t2.setDaemon(true);

        t2.start();

        try {

            Thread.sleep(5000);

        } catch (Exception e) {

            e.printStackTrace();

        }

        System.out.println("main Thread over");

    }

}

/**

 * join 等待该线程终止。将这个线程合并到主线程上 这个线程执行完才会执行主线程 另一种情况 如果 线程 A执行到了 线程 B 的 join

 * 方法,A就会等待 直到B执行万才会继续执行

 */

class JoinDemo implements Runnable {

    public static void main(String[] args) throws InterruptedException {

        JoinDemo jd = new JoinDemo();

        Thread t1 = new Thread(jd);

        Thread t2 = new Thread(jd);

        t1.start();

        // t1.join(); //放在这里 执行的顺序是 t1 全部执行完 主线程和t2抢夺执行权交替执行

        t2.start();

        t1.join();// 放在这里 执行顺序 是 t1 和t2 抢夺执行权 但是 只有当 t1全部执行完毕 main 才可以执行

        for (int i = 0; i < 100; i++) {

            Thread.sleep(10);

            System.out.println("the main thread------");

        }

    }

    @Override

    public void run() {

        for (int i = 0; i < 50; i++) {

            try {

                Thread.sleep(10);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            System.out.println(Thread.currentThread().getName() + "--" + i);

        }

    }

}

/**

 * wait 使放弃执行和锁(API 中叫 监视器)的所有权,需要 notify ,notifyAll 唤醒 在 一个 同步中如果 当前线程 wait

 * 别的线程可以进入这个 同步中 sleep 是当前线程放弃执行权但不放弃锁(API 中叫 监视器)的所有权, 在 一个 同步中如果 当前线程 sleep

 * 别的线程不可以进入这个 同步中

 *

 *

 * 同步的前提: 必须有两个或两个以上的线程 必须是多个线程使用同一个锁 必须保证同步中同以时间只有一个线程在运行

 *

 */

  class TestWait implements Runnable {

    int no = 100;

    @Override

    public void run() {

        // synchronized (this) {

        while (true) {

            if (no > 0) {

                System.out

                        .println(no + "--" + Thread.currentThread().getName());

                no--;

                notify();

                try {

                    System.out.println(Thread.currentThread().getName()

                            + "--wait");

                    wait();

                    System.out.println(Thread.currentThread().getName()

                            + "--weakup ");

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

        }

    }

    public static void main(String[] args) {

        TestWait t1 = new TestWait();

        new Thread(t1).start();

        new Thread(t1).start();

        new Thread(t1).start();

        new Thread(t1).start();

    }
}

/**

 * 测试死锁

 */

public class TestDeadLock  implements Runnable{

    public int flag=1;

    static Object o1 =new Object();

    static Object o2 = new Object();

    public static void main(String[] args) {

        TestDeadLock td=new TestDeadLock();

        td.flag=1;

        TestDeadLock td1= new TestDeadLock();

        td.flag=0;

        Thread t = new Thread(td);

        Thread t1 = new Thread(td1);

        

        t.start();

        t1.start();

    }

    @Override

    public void run() {

        if(flag==1 ){

            synchronized(o1){

                try {

                    Thread.sleep(100);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

                synchronized(o2){

                    System.out.println("1");

                }

            }

        }

        

        if(flag==0 ){

            synchronized(o2){

                try {

                    Thread.sleep(100);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

                synchronized(o1){

                    System.out.println("0");

                }

            }

        }

    }

}

a67a
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐