zoukankan      html  css  js  c++  java
  • Java之同步代码块处理继承Thread类的线程安全问题

    package com.atguigu.java;

    /**

    */
    /**
    * 使用同步代码块解决继承Thread类的方式的线程安全问题
    *
    * 例子:创建三个窗口卖票,总票数为100张.使用继承Thread类的方式
    *
    * 说明:在继承Thread类创建多线程的方式中,慎用this充当同步监视器,考虑使用当前类充当同步监视器。
    *
    */
    class Window2 extends Thread{


    private static int ticket = 100;

    private static Object obj = new Object();

    @Override
    public void run() {

    while(true){
    //正确的
    // synchronized (obj){
    synchronized (Window2.class){//Class clazz = Window2.class,Window2.class只会加载一次
    //错误的方式:this代表着t1,t2,t3三个对象
    // synchronized (this){

    if(ticket > 0){

    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    System.out.println(getName() + ":卖票,票号为:" + ticket);
    ticket--;
    }else{
    break;
    }
    }

    }

    }
    }


    public class WindowTest2 {
    public static void main(String[] args) {
    Window2 t1 = new Window2();
    Window2 t2 = new Window2();
    Window2 t3 = new Window2();


    t1.setName("窗口1");
    t2.setName("窗口2");
    t3.setName("窗口3");

    t1.start();
    t2.start();
    t3.start();

    }
    }
  • 相关阅读:
    Docker找不到私有nuget服务
    EF中字符串转数字排序
    一个简单的注册页面
    【转】【数据库SQL】SQL查询和替换含有回车,空格,TAB,等
    RGB颜色记录
    javascript中event.keycode
    java基础总结
    面试干货
    jQuery、实例大全
    使用Sql按日期条件查询
  • 原文地址:https://www.cnblogs.com/wpy188/p/12099885.html
Copyright © 2011-2022 走看看