| |

VerySource

 Forgot password?
 Register
Search
View: 781|Reply: 4

GUI thread safety issues

[Copy link]

3

Threads

10

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-3 21:30:01
| Show all posts |Read mode
In jdk5.0 sun, in order to ensure that the GUI code is created from a thread other than the event dispatch thread, it is recommended to use two methods defined by the SwingUtilties class: invokeAndWait () or invokeLater (), such as:
import java.awt. *;
import javax.swing. *;
public class JLabelDemo extends JApplet {
  public void init ()
    try {
        SwingUtilites.invokeAndWait (
           new Runnable () {
           public void run () {
               makeGUI ();
                              }
                                  }
      );
} catch (Exception e) {
            System.out.println ("Can't create" + e);
}
}
     private void makeGUI () {
        ImageIcon ii = new ImageIcon ("xxx.gif");
        JLabel jl = new JLabel ("France", ii, JLabel.CENTER);
        add (jl);
   }
}
_____________________________________________________________
Could you write it as:
  Synchronized private void makeGUI () {
  While not using nvokeAndWait () ??? /
Reply

Use magic Report

3

Threads

10

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-1-3 21:30:01
| Show all posts
Could you write it as:
  Synchronized private void makeGUI () {
  Also without invokeAndWait () ??? /
Reply

Use magic Report

3

Threads

10

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-1-11 10:45:01
| Show all posts
Top yourself
Reply

Use magic Report

3

Threads

10

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-7-21 21:00:02
| Show all posts
Why is there no answer? Doesn't everyone need invokewait()?
Reply

Use magic Report

1

Threads

51

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-7-21 22:30:01
| Show all posts
InvokeAndWait() or invokeLater() is a concept of dispatching thread and thread synchronization~
Can it be written as:
  Synchronized private void makeGUI(){
  Also don't use nvokeAndWait() ???/
You can implement a thread to complete this function yourself, but don’t you find it troublesome to achieve the functions of the two provided methods? And do you add Synchronized dry club? It is so difficult that multiple objects will access this at the same time Method?
Use the existing ones, why repeat inventing the wheel?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list