2019.02.27(java基础线程)

创建两个线程

方法1:

public class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }

    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        Thread t1 = new Thread(myThread,"t1");
        Thread t2 = new Thread(myThread,"t2");
        t1.start();
        t2.start();
    }
}

运行结果:
在这里插入图片描述
方法2:

public class MyThread2 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }

    public static void main(String[] args) {
        MyThread2 mt = new MyThread2();
        Thread t1 = new Thread(mt,"t1");
        Thread t2 = new Thread(mt,"t2");
        t1.start();
        t2.start();
    }
}

运行结果:
在这里插入图片描述

加入线程

创建第一个线程

public class Mythread3 implements Runnable {
    Thread t;
    @Override
    public void run() {
        for (int i = 0; i <10 ; i++) {
            try {
                t.join();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}

创建第二个线程

public class Mythread4 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i <10 ; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}

将第二个线程加入第一个线程

public class Text1 {
    public static void main(String[] args) {
        Mythread3 mt3 = new Mythread3();
        Mythread4 mt4 = new Mythread4();
        Thread t3 = new Thread(mt3,"t3");
        Thread t4 = new Thread(mt4,"t4");
        mt3.t = t4;
        t3.start();
        t4.start();
    }
}

运行结果:
在这里插入图片描述
总结:没有加入线程时,两个线程是并发(同时)运行的,有了加入线程,则被加入的线程进行等待,等待加入的线程执行完之后再执行被加入的线程

进程锁

不加进程锁的情况:
进程:

public class Mythread5 implements Runnable {
    int inno = 0;
    public void checkin(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+":卖了第"+ (++inno)+"张票");
    }
    @Override
    public void run() {
        checkin();
    }
}

测试:

public class Text2 {
    public static void main(String[] args) {
        Mythread5 mt = new Mythread5();
        Thread t1 = new Thread(mt,"t1");
        Thread t2 = new Thread(mt,"t2");
        Thread t3 = new Thread(mt,"t3");
        Thread t4 = new Thread(mt,"t4");
        Thread t5 = new Thread(mt,"t5");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
    }
}

运行结果:
在这里插入图片描述
在不加进程锁的情况下,五个进程同时运行很容易造成进程冲突,卖同一张票的情况
添加进程锁:关键字synchronized

public class Mythread5 implements Runnable {
    int inno = 0;
    public synchronized void checkin(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+":卖了第"+ (++inno)+"张票");
    }
    @Override
    public void run() {
        checkin();
    }
}

运行结果:
在这里插入图片描述
在添加了进程锁之后,当t1进程执行的时候其他进程是无法执行的,也就是说进程是一个一个执行的,不是并发执行的

死锁

进程1:

public class Mythread6 implements Runnable{
    Object o1;
    Object o2;
    @Override
    public void run() {
        System.out.println("获取锁对象o1");
        synchronized (o1){
            System.out.println("获取锁对象o2");
            synchronized (o2){
                System.out.println("释放锁对象o2");
            }
            System.out.println("释放锁对象o1");
        }
    }
}

进程2:

public class Mythread7 implements Runnable{
    Object o1;
    Object o2;
    @Override
    public void run() {
        System.out.println("获取锁对象o2");
        synchronized (o2){
            System.out.println("获取锁对象o1");
            synchronized (o1){
                System.out.println("释放锁对象o1");
            }
            System.out.println("释放锁对象o2");
        }
    }
}

测试类:

public class Text3 {
    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();
        Mythread6 mt6 = new Mythread6();
        mt6.o1 = o1;
        mt6.o2 = o2;
        Mythread7 mt7 = new Mythread7();
        mt7.o1 = o1;
        mt7.o2 = o2;
        Thread t1 = new Thread(mt6,"t1");
        Thread t2 = new Thread(mt7,"t2");
        t1.start();
        t2.start();
    }
}

死锁运行结果:
在这里插入图片描述
解除死锁:
使用wait和notify

public class Mythread6 implements Runnable{
    Object o1;
    Object o2;
    @Override
    public void run() {
        System.out.println("获取锁对象o1");
        synchronized (o1){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                o1.wait();//线程等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("获取锁对象o2");
            synchronized (o2){
                System.out.println("释放锁对象o2");
            }
            System.out.println("释放锁对象o1");
        }
    }
}
public class Mythread7 implements Runnable{
    Object o1;
    Object o2;
    @Override
    public void run() {
        System.out.println("获取锁对象o2");
        synchronized (o2){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("获取锁对象o1");
            synchronized (o1){
                o1.notify();//线程唤醒
                System.out.println("释放锁对象o1");
            }
            System.out.println("释放锁对象o2");
        }

    }
}

猜你喜欢

转载自blog.csdn.net/qq_34191426/article/details/87997090