Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 线程。睡眠(5000)不工作_Java_Multithreading - Fatal编程技术网

Java 线程。睡眠(5000)不工作

Java 线程。睡眠(5000)不工作,java,multithreading,Java,Multithreading,事实上,我正在做一个项目,它通过Thread.sleep(5000)在5秒内通过点击按钮生成一个对话并处理它 我在run方法下编写了以下代码 void showpopup(String p, String t) throws IOException { JPanel app = new JPanel(); popup = new JFrame(); // app.setSize(300,400); GridBagConstraints c1 = new GridB

事实上,我正在做一个项目,它通过Thread.sleep(5000)在5秒内通过点击按钮生成一个对话并处理它 我在run方法下编写了以下代码

void showpopup(String p, String t) throws IOException {
    JPanel app = new JPanel();
    popup = new JFrame();
    // app.setSize(300,400);
    GridBagConstraints c1 = new GridBagConstraints();
    app.setLayout(new GridBagLayout());
    app.setVisible(true);
    app.setBackground(new Color(39, 170, 225));

    popup.setUndecorated(true);
    popup.setSize(300, 100);
    popup.setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    JLabel header = new JLabel();
    header.setText(p);
    c1.gridx = 0;
    c1.gridy = 0;
    c1.weightx = 1.0f;
    c1.weighty = 1.0f;
    c1.insets = new Insets(5, 5, 5, 5);
    c1.fill = GridBagConstraints.BOTH;
    header.setFont(new Font("comfortaa", Font.BOLD, 15));
    app.add(header, c1);

    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.weightx = 0.3f;
    constraints.weighty = 0.3f;
    constraints.fill = GridBagConstraints.BOTH;

    popup.add(app, constraints);

    BufferedImage Butico = ImageIO.read(new File("C:/Users/utkarsh/Desktop/close.png"));
    JButton close = new JButton(new ImageIcon(Butico));
    close.setContentAreaFilled(false);
    close.setBorder(BorderFactory.createEmptyBorder());
    c1.gridx = 1;
    c1.weightx = 0f;
    c1.weighty = 0f;
    c1.insets = new Insets(5, 5, 5, 5);
    c1.fill = GridBagConstraints.NONE;
    c1.anchor = GridBagConstraints.NORTH;
    close.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent arg0) {
            popup.dispose();
        }
    });
    app.add(close, c1);

    JPanel cont = new JPanel(new GridBagLayout());
    // cont.setSize(arg0, arg1);
    String a = "hello";
    JLabel text = new JLabel("<HtML>" + t);
    text.setAlignmentX(Component.LEFT_ALIGNMENT);
    cont.setVisible(true);
    cont.setBackground(new Color(241, 242, 242));
    c1.gridx = 0;
    c1.gridy = 0;
    c1.weightx = 1.0f;
    c1.weighty = 1.0f;
    c1.insets = new Insets(5, 5, 5, 5);
    c1.anchor = GridBagConstraints.NORTH;
    c1.fill = GridBagConstraints.BOTH;
    cont.add(text, c1);
    constraints.gridx = 0;
    constraints.gridy += 1;
    constraints.weightx = 2.5f;
    constraints.weighty = 2.5f;
    constraints.fill = GridBagConstraints.BOTH;
    popup.add(cont, constraints);
    text.setFont(new Font("Comfortaa", Font.PLAIN, 12));
    text.setForeground(Color.DARK_GRAY);
    header.setForeground(Color.WHITE);
    popup.getRootPane().setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, new Color(188, 188, 188)));

    popup.setVisible(true);
    Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
    Insets th = Toolkit.getDefaultToolkit().getScreenInsets(popup.getGraphicsConfiguration());
    popup.setLocation(s.width - popup.getWidth() - 5, s.height - th.bottom - popup.getHeight() - 5);
    popup.setAlwaysOnTop(true);
    new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                popup.dispose();
            }

        }

    }.start();
}
void showpopup(字符串p,字符串t)引发IOException{
JPanel app=新的JPanel();
popup=newjframe();
//附录设置尺寸(300400);
GridBagConstraints c1=新的GridBagConstraints();
app.setLayout(新GridBagLayout());
app.setVisible(真);
附录.挫折背景(新颜色(39170225));
popup.setUndecorated(真);
设置大小(300100);
setLayout(新的GridBagLayout());
GridBagConstraints=新的GridBagConstraints();
JLabel头=新的JLabel();
header.setText(p);
c1.gridx=0;
c1.gridy=0;
c1.重量X=1.0f;
c1.重量=1.0f;
c1.插图=新插图(5,5,5,5);
c1.fill=GridBagConstraints.BOTH;
header.setFont(新字体(“comfortaa”,Font.BOLD,15));
附录添加(标题c1);
constraints.gridx=0;
constraints.gridy=0;
约束条件:权重x=0.3f;
约束条件。权重y=0.3f;
constraints.fill=gridbagsconstraints.BOTH;
添加(应用程序、约束);
BuffereImage Butico=ImageIO.read(新文件(“C:/Users/utkarsh/Desktop/close.png”);
JButton close=新JButton(新图像图标(Butico));
关闭。setContentAreaFilled(false);
close.setboorder(BorderFactory.createEmptyBorder());
c1.gridx=1;
c1.权重x=0f;
c1.重量Y=0f;
c1.插图=新插图(5,5,5,5);
c1.fill=GridBagConstraints.NONE;
c1.anchor=gridbag.NORTH;
close.addMouseListener(新的MouseAdapter(){
@凌驾
公共无效鼠标单击(鼠标事件arg0){
popup.dispose();
}
});
附录添加(关闭,c1);
JPanel cont=新的JPanel(新的GridBagLayout());
//继续设置大小(arg0、arg1);
字符串a=“你好”;
JLabel text=新的JLabel(“+t”);
text.setAlignmentX(组件左对齐);
cont.setVisible(真);
继续后退(新颜色(241、242、242));
c1.gridx=0;
c1.gridy=0;
c1.重量X=1.0f;
c1.重量=1.0f;
c1.插图=新插图(5,5,5,5);
c1.anchor=gridbag.NORTH;
c1.fill=GridBagConstraints.BOTH;
继续添加(文本,c1);
constraints.gridx=0;
约束条件.gridy+=1;
约束条件。权重x=2.5f;
约束条件。权重Y=2.5f;
constraints.fill=gridbagsconstraints.BOTH;
添加(续,约束);
text.setFont(新字体(“Comfortaa”,Font.PLAIN,12));
文本。设置前景(颜色。深灰色);
标题。设置前景(颜色。白色);
popup.getRootPane().setBorder(BorderFactory.createMatteBorder(1,1,1,1,新颜色(188188188188));
popup.setVisible(true);
维度s=Toolkit.getDefaultToolkit().getScreenSize();
Insets th=Toolkit.getDefaultToolkit().getScreenInsets(popup.getGraphicsConfiguration());
设置位置(s.width-popup.getWidth()-5,s.height-th.bottom-popup.getHeight()-5);
popup.setAlwaysOnTop(真);
新线程(){
@凌驾
公开募捐{
试一试{
睡眠(5000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}最后{
popup.dispose();
}
}
}.start();
}
现在,当我单击按钮时,它可以正常工作,但对于多个按钮单击,会出现多个Jdialog弹出窗口 只有最后一个在5秒后被处置


因此,要关闭每个JDialog框弹出窗口,我必须做的更改不是此方法的本地更改

popup = new JFrame();
因此,此代码不是线程安全的


每次单击时,都将用新创建的对象替换
弹出窗口
对象,因此
弹出窗口.dispose()将处理最后一个。

弹出窗口不是此方法的本地窗口

popup = new JFrame();
因此,此代码不是线程安全的


每次单击时,都将用新创建的对象替换
弹出窗口
对象,因此
弹出窗口.dispose()
将处理最后一个。

已编辑,请使用lookUse而不是
Thread.sleep()
。已编辑,请使用lookUse而不是
Thread.sleep()