简体   繁体   中英

java GUI multi-threading with socket

I am trying to make a GUI program which get data from TCP socket. The problem is when I set up the socket, and then start it, GUI program freeze. I want add one more function that stop thread by using click event.

As I know I have to use EDT, I tried but doesn't work. Is there anyone who can help me?

Here's my code.

  1. main

     public class ImageSplit { static ImageViewerFrame program; public static void main(String[] args) throws IOException { JPanel[] displayArray = new JPanel[100]; program = new ImageViewerFrame(); EventQueue.invokeLater(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub program.setVisible(true); } }); } 
  2. frame

     void createMouseMenu() { final PopupMenu menu = new PopupMenu(); MenuItem grid = new MenuItem("Grid"); MenuItem start = new MenuItem("Start"); MenuItem stop = new MenuItem("Stop"); menu.add(grid); menu.add(start); menu.add(stop); add(menu); mouseMenuHandler mousemenu = new mouseMenuHandler(); grid.addActionListener(mousemenu); start.addActionListener(mousemenu); stop.addActionListener(mousemenu); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (e.getModifiers() == InputEvent.BUTTON3_MASK) menu.show(e.getComponent(), e.getX(), e.getY()); } }); } class mouseMenuHandler implements ActionListener { getInfo info; public void actionPerformed(ActionEvent e) {// open grid setting if (e.getActionCommand().equals("Grid")) { gridSettingUI = new GridSetting(img); gridSettingUI.setVisible(true); } else if (e.getActionCommand().equals("Start")) { try { info = new getInfo(40000);//open socket info.run(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else if (e.getActionCommand().equals("Stop")) { System.out.println("stop"); info.stop(); } } } 
  3. socket Receiving

     import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class getInfo implements Runnable{ ServerSocket serverSocket; private boolean stopped=false; public getInfo(int port_num) throws IOException{ serverSocket = new ServerSocket(port_num); } @Override public void run() { // TODO Auto-generated method stub while(!stopped){ try { Socket socket = serverSocket.accept(); System.out.println(socket.getInetAddress()+ "connected"); InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); if(br.ready()) { String line = br.readLine(); System.out.println(line+"\\n"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void stop(){ stopped = true; } 

}

info = new getInfo(40000);//open socket
info.run();

In the lines above, you are NOT starting a thread, instead, you are just executing the run() method in the same thread (as it was any other method call).

Extend from Thread , then call info.start() .

See: Java Puzzlers: Traps, Pitfalls, and Corner Cases by Joshua Bloch and Neal Gafter, Puzzle #76 .

I wrote a library called Jexxus which lets you use sockets more easily. It will automatically do things on a different thread so your UI won't freeze. There's a little tutorial at the bottom of the page here: https://github.com/mirraj2/Jexxus/tree/master

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM