EdSheeranla 2017-01-12 06:18 采纳率: 0%
浏览 865

java中 当我用start启动线程,再用print输出我在run函数中操作的数字,发现没有改变

public class demo extends JFrame{

public static void main(String[] args) {
demo d=new demo();
}
public demo()
{   
    this.setLayout(new GridLayout(2,2));
    for(int i=0;i<4;i++)
    {
    TitledBorder border=new TitledBorder("售票窗口"+i);
    Mypanel mp=new Mypanel();
    mp.setBorder(border);
    this.add(mp);
    }
    this.setSize(400,300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);      
}

}
class Mypanel extends JPanel implements ActionListener{
JLabel jb1=null;
JLabel jb2 =null;
JButton jb=null;
int count =100;
public Mypanel(){
jb1=new JLabel();
jb2=new JLabel();
jb=new JButton("确认购票");
jb.addActionListener(this);
this.setLayout(new GridLayout(3,1));
this.add(jb1);
this.add(jb);
this.add(jb2);
}

public void actionPerformed(ActionEvent arg0) {
    Sale sale=new Sale();
    Thread t=new Thread (sale);
    try {
        t.sleep(50);
        t.start();

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println(sale.count);
    jb1.setText(sale.count+"");
    System.out.println(sale.count);
    System.out.println("完成一次操作");

}

}
class Sale implements Runnable{
static int count=100;
public void run(){
synchronized (this) {
if(count>0)
{
count--;

        }
    }

}

}


  • 写回答

2条回答

  • EdSheeranla 2017-01-12 06:24
    关注

    图片说明
    这是运行出来的结果截图,而用debug调试的时候两个输出来的结果都是99

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器