Pra*_*ect 1 java concurrency swing swingworker event-dispatch-thread
我一直在阅读关于Swing中的Java Concurrency,并且无法找到适合我情况的答案.
目标:让我的swing应用程序同时运行while循环,直到程序/应用程序退出.
问题:程序冻结(正如我之前预期的那样)并且无法使用.
尝试 - 解决方案:我试图在另一个线程上启动while循环,启动它,仍然没有.
码:
package stakeme;
import java.io.IOException;
import javax.swing.SwingUtilities;
public class StakeMeServer {
private static ServerSocket socket;
public static Thread t;
public static void main(String[] args) throws IOException {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ConsoleGUI("Server", 800, 400, false);
}
});
Thread t = new Thread(() -> {
System.out.println("1");
while (online) {
System.out.println("2");
}
});
t.setName("GameServer");
t.start();
}
}
Run Code Online (Sandbox Code Playgroud)
更新:通过使用SwingWorker类在后台执行我的while循环,我几乎可以做我需要做的事情.
码:
package stakeme;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class StakeMeServer {
private static AtomicBoolean online = new AtomicBoolean(false);
public static GameServerLoop gsl = new GameServerLoop();
public static void main(String[] args) throws IOException {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
new ConsoleGUI("Server", 800, 400, false);
}
});
//this should always be running during the life of the program
gsl.execute();
}
static class GameServerLoop extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() {
System.out.println("1");
while (true) {
while (online.get()) {
System.out.println("2");
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
一切正常,一个错误.使用该System.out.println()方法以处理速度(while循环)运行此程序将使程序有些 - 没有响应.单击停止按钮将起作用..最终.由于大量的System.out.println()呼叫,似乎所有东西都备份了.

所以你不完整的例子留下了很多未回答的问题,基本上就是这样的......
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class StakeMeServer {
private ServerSocket socket;
private Thread t;
private AtomicBoolean online = new AtomicBoolean(false);
public static void main(String[] args) throws IOException {
new StakeMeServer();
}
public StakeMeServer() {
SwingUtilities.invokeLater(new Runnable() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
open();
Thread t = new Thread(() -> {
System.out.println("1");
while (online.get()) {
System.out.println("2");
}
});
t.setDaemon(true);
t.setName("GameServer");
t.start();
}
public void open() {
System.out.println("Starting Server...");
online.set(true);
System.out.println("Server Online");
}
public void close() {
System.out.println("Stopping Server...");
online.set(false);
System.out.println("Server Offline");
}
public class TestPane extends JPanel {
private int x = 0;
private int delta = 4;
private JLabel state;
public TestPane() {
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
x += delta;
if (x + 10 > getWidth()) {
x = getWidth() - 10;
delta *= -1;
} else if (x < 0) {
x = 0;
delta *= -1;
}
state.setText(Boolean.toString(online.get()));
repaint();
}
});
timer.start();
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
state = new JLabel(Boolean.toString(online.get()));
add(state, gbc);
JButton btn = new JButton("Stop");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btn.setEnabled(false);
close();
}
});
add(btn, gbc);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
g2d.fillOval(x, (getHeight() / 2) - 5, 10, 10);
g2d.dispose();
}
}
}
Run Code Online (Sandbox Code Playgroud)
工作得很好.
我使用了一个AtomicBoolean而不是boolean防止线程之间可能的脏读/写,并成为Thread一个守护程序线程,因此应用程序可以关闭,线程不会阻止JVM终止.
就个人而言,我倾向于使用a SwingWorker,因为它提供的功能允许您简单地将更新同步回UI
有关更多详细信息,请查看Swing和Worker Threads中的Concurrency 以及SwingWorker
| 归档时间: |
|
| 查看次数: |
1143 次 |
| 最近记录: |