gpt4 book ai didi

java - 在java中使用多线程?

转载 作者:行者123 更新时间:2023-12-01 18:05:33 26 4
gpt4 key购买 nike

我正在尝试创建 4 个汽车对象,每个对象都在单独的线程上,但我无法弄清楚如何让每辆车以用户输入的不同速度移动。

这是我的主程序:

public class Racecar extends JApplet {

public Racecar() {
setLayout(new GridLayout(5, 1));
JPanel panel1 = new JPanel();
JLabel car1 = new JLabel("Car 1:");
panel1.add(car1);
JTextField jtfcar1 = new JTextField(3);
panel1.add(jtfcar1);
JLabel car2 = new JLabel("Car 2:");
panel1.add(car2);
JTextField jtfcar2 = new JTextField(3);
panel1.add(jtfcar2);
JLabel car3 = new JLabel("Car 3:");
panel1.add(car3);
JTextField jtfcar3 = new JTextField(3);
panel1.add(jtfcar3);
JLabel car4 = new JLabel("Car 4:");
panel1.add(car4);
JTextField jtfcar4 = new JTextField(3);
panel1.add(jtfcar4);
add(panel1, BorderLayout.NORTH);

JPanel panel2 = new JPanel();
panel2.setBorder(BorderFactory.createLineBorder(Color.black));
DrawCar car = new DrawCar(1);
panel2.add(car);
add(panel2);

JPanel panel3 = new JPanel();
panel3.setBorder(BorderFactory.createLineBorder(Color.black));
DrawCar car10 = new DrawCar(2);
panel3.add(car10);
add(panel3);

JPanel panel4 = new JPanel();
panel4.setBorder(BorderFactory.createLineBorder(Color.black));
DrawCar car11 = new DrawCar(3);
panel4.add(car11);
add(panel4);

JPanel panel5 = new JPanel();
panel5.setBorder(BorderFactory.createLineBorder(Color.black));
DrawCar car12 = new DrawCar(4);
panel5.add(car12);
add(panel5);

}

public static void main(String[] args) {
Racecar applet = new Racecar();
applet.init();
applet.start();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("");
frame.getContentPane().add(applet, BorderLayout.CENTER);
frame.setSize(400, 320);
frame.setVisible(true);


// Create tasks
Runnable race1 = new DrawCar(1);
Runnable race2 = new DrawCar(2);
Runnable race3 = new DrawCar(3);
Runnable race4 = new DrawCar(4);

// Create threads
Thread thread1 = new Thread(race1);
Thread thread2 = new Thread(race2);
Thread thread3 = new Thread(race3);
Thread thread4 = new Thread(race4);

// Start threads
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}

}

这是我的 DrawCar 方法:

public class DrawCar extends JPanel implements ActionListener, Runnable {
private int delay = 10;
private Timer timer = new Timer(delay, this);
private int x1 = 20;
private int y1 = 22;
int r = (int)(Math.random()*256);
int q =(int)(Math.random()*256);
int b = (int)(Math.random()*256);
Color color = new Color(r, q, b);

public DrawCar(int x) {
timer.start();
}

public void actionPerformed(ActionEvent e) {
repaint();
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
g.fillRoundRect(x1, y1, 75, 20, 10, 10);
g.fillArc(x1, y1 - 10, 50, 15, 0, 180);
g.fillOval(x1 + 2, y1 + 10, 20,20);
g.fillOval(x1 + 40, y1 + 10, 20, 20);
if(x1 > getWidth()) {
x1 = -100;
}
x1 += 1;

repaint();
}

public Dimension getPreferredSize() {
return new Dimension(400, 320);
}

private class TimerListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
repaint();
}
}

public void run() {

try {

//int x = Racecar.getSpeed();

Thread.sleep(1500);

}
catch (InterruptedException ex) {
}

}

}

最佳答案

I'm trying to create 4 car objects that are each on separate threads

嗯,这里有一个问题。有几个原因,可能最重要的是 Swing 不是线程安全的,因此您面临着竞争条件和不同线程之间脏读/写的风险,这可能导致“模型”与 UI 不同步。

相反,您应该有一个线程来更新所有汽车,每辆车都是一个对象,将拥有自己的速度属性,该属性将确定每个周期会发生多少变化。

我不会为每辆车创建一个 JPanel,而是使用一个 JPanel 来负责绘制所有汽车。这将为您提供更好的性能,但也更容易处理。

我首先看一下 Concurrency in Swing了解更多详情。

由于 Swing 的单线程性质,我还想看看 How to use Swing Timers而不是尝试使用线程。

这是一个非常糟糕的主意......

protected void paintComponent(Graphics g) {
super.paintComponent(g);
//...
if(x1 > getWidth()) {
x1 = -100;
}
x1 += 1;
repaint();
}

绘制可能因多种不同原因而发生,其中许多原因是您无法控制的,因此您的对象移动得更快不是因为线程正在做什么,而是因为系统每次绘制时正在做什么组件,所以基本上,它失去了控制。

绘画就是为了绘画,没有别的,你永远不应该在绘画方法中更新组件的状态。

此外,您永远不应该从 Paint 方法中(直接或间接)调用 repaint,这会很快导致失控的重绘周期,最终会消耗所有 CPU 周期。

也许更像是......

Race

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
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);
}
});
}

public class TestPane extends JPanel {

private List<Car> cars;

public TestPane() {
cars = new ArrayList<>(4);
Color[] colors = new Color[]{Color.RED, Color.GREEN, Color.BLUE, Color.MAGENTA};
int yPos = 0;
int speed = 1;
for (Color color : colors) {
Car car = new Car(new Point(0, yPos += 30), speed++, color);
cars.add(car);
}
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (Car car : cars) {
car.update(getSize());
}
repaint();
}
});
timer.start();
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Car car : cars) {
car.paint(g2d);
}
g2d.dispose();
}

}

public class Car {
private Point location;
private Dimension size;

private int speed;
private Color color;

public Car(Point location, int speed, Color color) {
this.location = location;
this.speed = speed;
this.color = color;

size = new Dimension(20, 20);
}

public void update(Dimension bounds) {
location.x += speed;
if (location.x + size.width > bounds.width) {
location.x = bounds.width - size.width;
speed = 0;
}
}

public void paint(Graphics2D g2d) {
g2d.setColor(color);
g2d.fill(new Rectangle(location, size));
}

}

}

会有帮助

关于java - 在java中使用多线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36734707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com