休眠一个iPOJO组件,全部禁用 [英] Sleep a iPOJO component, all disable

查看:96
本文介绍了休眠一个iPOJO组件,全部禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于iPOJO的简单问题.

I have a simple question relating to iPOJO.

当组件iPOJO休眠时,所有其余组件也将被禁用,尽管它们之间没有依赖关系.为什么?这是一个示例:

When a component iPOJO sleeps, all remaining components will also disable although there are not dependencies between them. Why? Here's an example:

组件1:

@Component(name="frame1", immediate=true)
@Instantiate(name="iframe1")
public class Frame1 implements Runnable{

    String str;
    Label lb = new Label();
    TextField tf = new TextField();
    Frame fr;
public void run() {
    fr = new Frame("Frame1");
    fr.setLayout(new BorderLayout());
    fr.setSize(230, 200);
    fr.setLocation(900,250);
    fr.add(tf, BorderLayout.NORTH);
    lb.setText("Result");
    fr.add(lb, BorderLayout.CENTER);
    Panel pn = new Panel();
    fr.add(pn, BorderLayout.SOUTH);
    pn.setLayout(new GridLayout(1,4,1,1));
    Button bt = new Button("Printer 1");
    pn.add(bt);
    bt.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                System.out.println("start sleep");
                Thread.sleep(5000);
                System.out.println("stop sleep");
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

    });

    fr.setVisible(true);
 }
 @Validate
 public void start() {
     //this.delayService = dls;
     Thread th = new Thread(this);
     th.start();
 }
 @Invalidate
 public void stop() {
     System.out.println("stop");
     fr.setVisible(false);
 }
}    

组件2:

@Component(name="frame2", immediate=true)
@Instantiate(name="iframe2")
public class Frame2 implements Runnable{

String str;
Label lb = new Label();
TextField tf = new TextField();
Frame fr;
public void run() {
    System.out.println("start component 2");
    fr = new Frame("Frame2");
    fr.setLayout(new BorderLayout());
    fr.setSize(230, 200);
    fr.setLocation(900,250);
    fr.add(tf, BorderLayout.NORTH);
    lb.setText("Result");
    fr.add(lb, BorderLayout.CENTER);

    Panel pn = new Panel();
    fr.add(pn, BorderLayout.SOUTH);
    pn.setLayout(new GridLayout(1,4,1,1));
    Button bt = new Button("Printer 2");
    pn.add(bt);
    bt.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("in 2");
        }
    });

    fr.setVisible(true);
}
@Validate
 public void start() throws Exception {
     //this.delayService = dls;
     System.out.println("start thread 2");
     Thread th = new Thread(this);
     th.start();

     //fr.setVisible(true);
 }
 @Invalidate
 public void stop() throws Exception {
     System.out.println("stop");
     fr.setVisible(false);
 }
}

两个组件已部署并正在运行.有两个独立的组件.但是我单击打印机1"按钮. "frame1"组件在5秒内处于休眠状态.在这5秒钟内,我无法单击"frame2"组件的打印机2".

Two components are deployed and running. There are two independent components. But I click the "Printer 1" button. "frame1" component is sleeping during 5s. And during these 5 seconds, i can't click "Printer 2" of "frame2" component.

推荐答案

这不是ipojo问题. Swing使用一个线程(并且仅使用一个线程)来调度诸如点击之类的事件.当您单击第一个按钮时,swing在此线程中运行您的actionPerformed.此方法使您的线程休眠5秒钟.这意味着负责事件处理的线程在此期间无法执行任何操作.这就是为什么您的程序不响应第二次单击的原因.

This is not an ipojo issue. Swing uses one thread (and only one thread) in order to dispatch events such as clicks. When you click your first button, swing runs your actionPerformed in this thread. This method puts your thread to sleep for 5 seconds. This means that the thread responsible for event handling cannot do anything during this time. This is why your program does not respond to your second click.

每当您在swing(以及osgi)中进行较长的计算时,通常最好在一个单独的线程中运行代码,以避免阻塞执行(这里您有一个无用的Thread.sleep(),但是我想您可能会有一个http请求或可能需要很长时间的任何内容).您可能应该使用执行器服务或类似的东西.

Whenever you have a long computation in swing (and also osgi), it is often a good idea to run your code in a separate thread in order to avoid blocking the execution (here you have a useless Thread.sleep() but I guess you could have an http request or anything that may take a long time instead). You should probably use an executor service or anything similar.

这篇关于休眠一个iPOJO组件,全部禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆