day15

今天是复习之前学习的线程相关内容

线程的建立

public class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println("线程运行");


    }
}
public class Test {
    public static void main(String[] args) {
        MyThread t = new MyThread();
        System.out.println("线程运行开始");
        t.start();
        System.out.println("线程运行结束");
    }
}

运行结果:这里写图片描述

线程的同步运行

public class Mythread extends Thread{
    public Mythread(){

    }
    public Mythread(String name){
        super(name);
    }
    private int tickets = 100;
    @Override
    public void run() {
        while(tickets>0){
            System.out.println(getName()+"卖出了第"+tickets--+"张票");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
public class Test {
    public static void main(String[] args) {
        Mythread t1 = new Mythread("售票员1");
        Mythread t2 = new Mythread("售票员2");
        Mythread t3 = new Mythread("售票员3");
        Mythread t4 = new Mythread("售票员4");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}

运行结果:这里写图片描述

用Runnable实现多线程

包括线程锁和join()方法
import javax.swing.plaf.SliderUI;

public class MyRunnable implements Runnable{
    private int tickets = 21;
    private String s = "";
    @Override
    public void run() {
        while(tickets>0){
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(tickets==0){
                break;
            }
            synchronized(s){
                System.out.println(Thread.currentThread().getName()+"卖出第"+tickets--+"张票");
            }
        }
    }

}
public class Test {
    public static void main(String[] args) {
        MyRunnable runnable = new MyRunnable();
        Thread t1 = new Thread(runnable, "售票员1");
        Thread t2 = new Thread(runnable, "售票员2");
        Thread t3 = new Thread(runnable, "售票员3");
        Thread t4 = new Thread(runnable, "售票员4");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}

运行结果:这里写图片描述

public class JoinTest {
    public static void main(String[] args) {
        MyRunnable runnable = new MyRunnable();
        Thread t0 = new Thread(runnable);
        t0.start();
        try {
            t0.join(0);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("程序结束");
    }
}

运行结果:这里写图片描述

银行取钱的算法

public class GetMoney implements Runnable{
    private int money = 3000;
    @Override
    public void run() {

        while(money>0){
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(money==0){
                break;
            }
            synchronized ("") {
                money = money-100;
                System.out.println(Thread.currentThread().getName()+"取了第"+(3000-money)/100+"个100元,剩余"+money+"元");
            }
        }
    }

}
public class Test {
    public static void main(String[] args) {
        GetMoney get = new GetMoney();
        Thread t1 = new Thread(get, "一号");
        Thread t2 = new Thread(get, "二号");
        t1.start();
        t2.start();
    }
}

运行结果:这里写图片描述

死锁

public class Runnable1 implements Runnable{
    private String lock1 = "123";
    private String lock2 = "456";
    @Override
    public void run() {
        synchronized (lock1) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("等待lock2");
            synchronized (lock2) {

            }
        }
    }

}
public class Runnable2 implements Runnable{
    private String lock1 = "123";
    private String lock2 = "456";
    @Override
    public void run() {
        synchronized (lock2) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("等待lock1");
            synchronized (lock1) {

            }
        }
    }

}
public class Test {
    public static void main(String[] args) {
        Runnable1 run1 = new Runnable1();
        Runnable2 run2 = new Runnable2();
        Thread t1 = new Thread(run1);
        Thread t2 = new Thread(run2);
        t1.start();
        t2.start();
    }
}

运行结果:这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值