Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

java - Using Thread with Vaadin?

I'm trying to use Thread in my project to send emails. When I click on a Button, a Thread is started and a ProgressBar is displayed. As soon as all mails are sent, the ProgressBar doesn't disappear.

This is my code:

Button btnSendMail = new Button("Mail");
btnSendMail.addClickListener(this);
@Override
public void buttonClick(ClickEvent event) {     
    if(event.getButton() == btnSendMail){   
            sendMail();
    }
}
}    

private void sendMail(){
     List<String> list = new ArrayList<String>();
     list.add("[email protected]");
     list.add("[email protected]");
     list.add("[email protected]");

     new Thread(){
         public void run(){
             while(!isInterrupt()){
                 progressbar.setVisible(true);
                 for(String send : list){
                     new SendMailClass(send); //javamail class
                 }           
                 progressbar.setVisible(false);
                 interrupt();
    }   
}.start();


}

How can I control visibility of the ProgressBar from a separated Thread?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To update UI elements from a background thread, you have to activate either push or polling.

The documentation can be found in the vaadin book.

https://vaadin.com/de/book/vaadin7/-/page/advanced.push.html

In addition to enabling push, you also need to synchronize access to the UI elements as described in section "11.16.3. Accessing UI from Another Thread"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...