Java 停止线程和在何处使用锁
我的问题是在哪里使用这些线程上的锁。当我让我的圈子互相竞赛时,当一个人到达终点时,程序应该停止并宣布获胜。相反,它完成了所有的圆圈到终点线,我相信这是因为它们都共享相同的锁,甚至可能是资源 这是我的三门课。任何帮助都将不胜感激Java 停止线程和在何处使用锁,java,multithreading,Java,Multithreading,我的问题是在哪里使用这些线程上的锁。当我让我的圈子互相竞赛时,当一个人到达终点时,程序应该停止并宣布获胜。相反,它完成了所有的圆圈到终点线,我相信这是因为它们都共享相同的锁,甚至可能是资源 这是我的三门课。任何帮助都将不胜感激 import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.GridLayout;
public class HorsePanel extends JFrame {
private JPanel panel;
private JButton reset;
private JButton quit;
private JButton run;
private ActionListener listener;
public static final int FRAME_WIDTH = 500;
public static final int FRAME_HEIGHT = 400;
private TrackPane trackPane;
public HorsePanel(TrackPane t) {
trackPane = t;
createPanel();
createRunRace();
createQuit();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
public void createRunRace() {
class RunRace implements ActionListener {
public void actionPerformed(ActionEvent rightEvent) {
run.setEnabled(false);
trackPane.start();
}
}
ActionListener a = new RunRace();
this.run.addActionListener(a);
}
public void createQuit() {
class QuitRace implements ActionListener {
public void actionPerformed(ActionEvent rightEvent) {
System.exit(0);
}
}
ActionListener b = new QuitRace();
this.quit.addActionListener(b);
}
public void createReset() {
class ResetRace implements ActionListener {
public void actionPerformed(ActionEvent rightEvent) {
//trackPane.resetCoordinates;
run.setEnabled(true);
}
}
ActionListener c = new ResetRace();
this.reset.addActionListener(c);
}
public void createPanel() {
panel = new JPanel(new BorderLayout());
trackPane = new TrackPane();
this.run = new JButton("Run Race");
this.quit = new JButton("Quit");
this.reset = new JButton("Reset");
JPanel topPanel = new JPanel();
topPanel.setLayout(new GridLayout(1, 3));
topPanel.add(run);
topPanel.add(reset);
topPanel.add(quit);
panel.add(topPanel, BorderLayout.NORTH);
panel.add(trackPane, BorderLayout.CENTER);
add(panel);
}
}
import javax.swing.JFrame;
public class HorseTester {
public static TrackPane t = new TrackPane();
public static void main(String[] args) {
new HorseTester();
}
public HorseTester() {
JFrame frame = new HorsePanel(t);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class HorseThread implements Runnable {
public static final int X_START = 10;
public static final int Y_START = 20;
private boolean RUNNING = true;
public int FINISH_LINE = 450;
private Horse horse;
private int xpos, ypos;
private TrackPane track;
public JFrame frame = new JFrame();
public int id;
public HorseThread(TrackPane track, int offset) {
xpos = X_START;
id = offset;
// Spaces the horses apart
ypos = Y_START * offset * 3;
horse = new Horse(xpos, ypos);
this.track = track;
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
horse.draw(g2);
}
/**
* Run method that thread executes and makes horses go across the screen
* racing.
*/
public void run() {
while (RUNNING) {
//varying the x position movement
horse.setX(xpos += (int) (Math.random() * 10 + 1), id);
// Sleeping the thread
track.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (xpos >= FINISH_LINE) {
RUNNING = false;
}
}
}
}
public class Horse {
private int xTop, xStart;
private int yTop, yStart;
public static final int RING_WIDTH = 20, FINISH_LINE=450;
public JFrame frame = new JFrame();
public Lock lockThing = new ReentrantLock();
public Horse(int x, int y) {
xTop = x;
yTop = y;
xStart = x;
yStart = y;
}
public void setX(int dx, int id) {
try{
lockThing.lock();
xTop=dx;
if (dx >= FINISH_LINE){
JOptionPane.showMessageDialog(frame, id + " Won");
}
}finally{ lockThing.unlock();}
}
public void draw(Graphics2D g2) {
Ellipse2D.Double horse = new Ellipse2D.Double(xTop, yTop, RING_WIDTH,
RING_WIDTH);
g2.setColor(Color.RED);
g2.fill(horse);
g2.setColor(Color.BLUE);
g2.draw(horse);
}
}
public class TrackPane extends JPanel {
private static final int NUM_OF_HORSES = 5;
private ArrayList<HorseThread> horses = new ArrayList<HorseThread>();
private ArrayList<Thread> threads = new ArrayList<Thread>(25);
public TrackPane() {
setBackground(Color.WHITE);
reset();
}
public void reset() {
// Should dispose of any running threads...
horses.clear();
// Allocating the memory for horses
for (int i = 0; i < NUM_OF_HORSES; i++) {
horses.add(new HorseThread(this, i + 1));
}
}
public void start() {
// Should dispose of any running threads...
threads.clear();
for (int i = 0; i < horses.size(); i++) {
Thread thread = new Thread(horses.get(i));
thread.start();
threads.add(thread);
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (HorseThread horse : horses) {
horse.paint(g);
}
}
}
导入java.awt.BorderLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JButton;
导入java.awt.GridLayout;
公共类HorsePanel扩展JFrame{
私人JPanel小组;
专用按钮复位;
私人JButton退出;
私人按钮运行;
私人行动听者;
公共静态最终整数帧_宽度=500;
公共静态最终内部框架高度=400;
专用轨迹窗格轨迹窗格;
公共配电盘(轨道面板t){
轨迹窗格=t;
createPanel();
createRunRace();
createQuit();
设置尺寸(框宽、框高);
}
public void createRunRace(){
类RunRace实现ActionListener{
已执行的公共作废操作(ActionEvent rightEvent){
run.setEnabled(false);
trackPane.start();
}
}
ActionListener a=新的RunRace();
this.run.addActionListener(a);
}
public void createQuit(){
类QuitRace实现ActionListener{
已执行的公共作废操作(ActionEvent rightEvent){
系统出口(0);
}
}
ActionListener b=新QuitRace();
this.quit.addActionListener(b);
}
public void createReset(){
类ResetRace实现ActionListener{
已执行的公共作废操作(ActionEvent rightEvent){
//trackPane.reset坐标;
run.setEnabled(true);
}
}
ActionListener c=新的ResetRace();
this.reset.addActionListener(c);
}
public void createPanel(){
panel=newjpanel(newborderlayout());
trackPane=新的trackPane();
this.run=新的JButton(“跑步比赛”);
this.quit=新的JButton(“quit”);
this.reset=新的JButton(“reset”);
JPanel-topPanel=新的JPanel();
topPanel.setLayout(新网格布局(1,3));
topPanel.add(运行);
topPanel.添加(重置);
topPanel.add(退出);
面板。添加(topPanel,BorderLayout.NORTH);
panel.add(轨迹窗格,BorderLayout.CENTER);
添加(面板);
}
}
导入javax.swing.JFrame;
公共级测马仪{
public static TrackPane t=new TrackPane();
公共静态void main(字符串[]args){
新马力测试仪();
}
公共测马仪(){
JFrame=新的马鞍形板(t);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.util.concurrent.locks.Lock;
导入java.util.concurrent.locks.ReentrantLock;
导入javax.swing.JFrame;
导入javax.swing.JOptionPane;
公共类HorseThread实现了Runnable{
公共静态最终整数X_开始=10;
公共静态最终整数Y_开始=20;
私有布尔运行=真;
公共int饰面线=450;
私家马;
私人int XPO、YPO;
专用轨道板轨道;
公共JFrame=新JFrame();
公共int id;
公共HorseThread(轨迹窗格轨迹,int偏移){
xpos=X_开始;
id=偏移量;
//把马隔开
ypos=Y_开始*偏移*3;
马=新马(xpos、YPO);
this.track=轨道;
}
公共空间涂料(图g){
图形2d g2=(图形2d)g;
马。画(g2);
}
/**
*线程执行并使马穿过屏幕的Run方法
*比赛。
*/
公开募捐{
(跑步时){
//改变x位置移动
setX(xpos+=(int)(Math.random()*10+1),id);
//睡线
track.repaint();
试一试{
睡眠(100);
}捕捉(中断异常e){
e、 printStackTrace();
}
如果(xpos>=完成线){
运行=错误;
}
}
}
}
公马{
私有intxtop,xStart;
伊斯塔特国际私人酒店;
公共静态最终整环宽度=20,完工线=450;
公共JFrame=新JFrame();
public Lock lockThing=new ReentrantLock();
公马(整数x,整数y){
xTop=x;
yTop=y;
xStart=x;
yStart=y;
}
公共void setX(int-dx,int-id){
试一试{
lockThing.lock();
xTop=dx;
如果(dx>=终点线){
showMessageDialog(框架,id+“赢得”);
}
}最后{lockThing.unlock();}
}
公共空白绘图(图形2D g2){
Ellipse2D.Double horse=新的Ellipse2D.Double(xTop,yTop,环宽,
环宽);
g2.设置颜色(颜色为红色);
g2.填充(马);
g2.setColor(颜色为蓝色);
g2.画(马);
}
}
公共类TrackPane扩展了JPanel{
私有静态最终int NUM_马=5;
private ArrayList horses=new ArrayList();
私有ArrayList线程=新ArrayList(25);
公共轨道窗格(){
挫折地面(颜色:白色);
重置();
}
公共无效重置(){
//应该处理所有正在运行的线程。。。
马。清除();
//为马分配内存
for(inti=0;i
您需要在线程之间共享运行变量
private boolean RUNNING = true;
最快的方法(但不是最好的方法)是将其更改为静态:
private static volatile boolean RUNNING = true;
更恰当、更幸运的方法是创建一个
JOptionPane.showMessageDialog(new JFrame(), id + " Won");
class HorseThread implements Runnable {
private static volatile boolean RUNNING = true;
public static final int X_START = 10;
public static final int Y_START = 20;
public int FINISH_LINE = 450;
private Horse horse;
private int xpos, ypos;
private TrackPane track;
public JFrame frame = new JFrame();
public int id;
public HorseThread(TrackPane track, int offset) {
xpos = X_START;
id = offset;
// Spaces the horses apart
ypos = Y_START * offset * 3;
horse = new Horse(xpos, ypos);
this.track = track;
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
horse.draw(g2);
}
/**
* Run method that thread executes and makes horses go across the screen
* racing.
*/
public void run() {
while (RUNNING) {
//varying the x position movement
horse.setX(xpos += (int) (Math.random() * 10 + 1), id);
// Sleeping the thread
track.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (xpos >= FINISH_LINE) {
RUNNING = false;
JOptionPane.showMessageDialog(new JFrame(), id + " Won");
}
}
}
}
class Horse {
private int xTop, xStart;
private int yTop, yStart;
public static final int RING_WIDTH = 20, FINISH_LINE=450;
public JFrame frame = new JFrame();
public Lock lockThing = new ReentrantLock();
public Horse(int x, int y) {
xTop = x;
yTop = y;
xStart = x;
yStart = y;
}
public void setX(int dx, int id) {
try{
lockThing.lock();
xTop=dx;
}finally{ lockThing.unlock();}
}
public void draw(Graphics2D g2) {
Ellipse2D.Double horse = new Ellipse2D.Double(xTop, yTop, RING_WIDTH,
RING_WIDTH);
g2.setColor(Color.RED);
g2.fill(horse);
g2.setColor(Color.BLUE);
g2.draw(horse);
}
}