Java实现多线程的两种主要方式

看不懂拉到最后有解释
1.继承Thread类,重写run方法

public class ExtendThread extends Thread{
    static int times = 100;
    static Object key = "aa";//值是任意的

    public ExtendThread(String name){
        super(name);
    }

    @Override
    public void run() {

        while (times>0) {
//            synchronized (key) {
                if (times > 0) {
                    System.out.println(getName()+"卖出了第"+times+"张票");
                    times--;
                } else {
                    System.out.println("无票了");
                }
            }
//        }
    }

    public static void main(String[] args) {
        ExtendThread extendThread1 = new ExtendThread("窗口1");
        ExtendThread extendThread2 = new ExtendThread("窗口2");
        ExtendThread extendThread3 = new ExtendThread("窗口3");

        extendThread1.start();
        extendThread2.start();
        extendThread3.start();
    }

}

2.实现Runnable接口,重写run方法

public class RunnableThread implements Runnable{

    private int times = 30; //    static int times = 30;
    static Object key = "123";

    @Override
    public void run() {
        while (times>0){
            synchronized (key) {
                if (times > 0) {
                    System.out.println(Thread.currentThread().getName()+"卖出了第"+times+"张票");
                    times--;
                }else {
                    System.out.println("无票了哈哈");
                }
            }
        }
    }

    public static void main(String[] args) { //        Runnable runnableThread = new RunnableThread();
        RunnableThread runnableThread = new RunnableThread();
        new Thread(runnableThread).start();
        new Thread(runnableThread).start();
        new Thread(runnableThread).start();

    } }

//解释

public class test extends Thread{
    // 为了保持票数的一致,票数要静态
    static int tick = 20;
    // 通过构造方法给线程名字赋值
    public test(String name) {
        super(name);// 给线程名字赋值
    }

    // 创建一个静态钥匙
    static Object ob = "aa";//值是任意的
    // 重写run方法,实现买票操作
    @Override
    public void run() {
        while (tick > 0) {
            synchronized (ob) {// 这个很重要,必须使用一个锁,
                // 进去的人会把钥匙拿在手上,出来后才把钥匙拿让出来
                if (tick > 0) {
                    System.out.println(getName() + "卖出了第" + tick + "张票");
                    tick--;
                } else {
                    System.out.println("票卖完了");
                }
            }
//            try {
//                sleep(1000);//休息一秒
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
    }

    public static void main(String[] args) {
        test test1 = new test("窗口1");
        test test2 = new test("窗口2");
        test test3 = new test("窗口3");

        test1.start();
        test2.start();
        test3.start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值