Java 多线程访问多个对象
我有一个带有四个文本框的GUI窗口,我的代码允许用户创建多个线程。我希望对于每个线程,线程找到一个可用的文本区域并完成其输出语句。我一次只能执行一个线程,但问题是该线程访问所有文本区域并写入它们 用Java编写的代码如下:Java 多线程访问多个对象,java,multithreading,Java,Multithreading,我有一个带有四个文本框的GUI窗口,我的代码允许用户创建多个线程。我希望对于每个线程,线程找到一个可用的文本区域并完成其输出语句。我一次只能执行一个线程,但问题是该线程访问所有文本区域并写入它们 用Java编写的代码如下: import java.io.*; import java.awt.*; import java.awt.event.*; import java.util.Random; class thread_action extends Thread { private s
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
class thread_action extends Thread
{
private semaphores view;
public thread_action(int threadNumber, semaphores view)
{
super(" " + threadNumber);
this.view = view;
}
public void land()
{
//Try to land on Runway 1
synchronized(view.output1) {
view.output1.append("Plane" + getName() + " approach runway.\n");
try { Thread.sleep (2500); }
catch ( InterruptedException e ) {}
view.output1.append("Plane" + getName() + " descending ...\n");
try { Thread.sleep (2500); }
catch ( InterruptedException e ) { }
view.output1.append("Plane" + getName() + " clearing runway and taxi to apron.\n\n");
}
//Landing completed
//Try to land on Runway 2
synchronized(view.output2) {
view.output2.append("Plane" + getName() + " approach runway.\n");
try { Thread.sleep (1000); }
catch ( InterruptedException e ) { }
view.output2.append("Plane" + getName() + " descending ...\n");
try { Thread.sleep (1000); }
catch ( InterruptedException e ) { }
view.output2.append("Plane" + getName() + " clearing runway and taxi to apron.\n\n");
}
//Landing completed
//Try to land on Runway 3
synchronized(view.output3) {
view.output3.append("Plane" + getName() + " approach runway.\n");
try { Thread.sleep (1000); }
catch ( InterruptedException e ) { }
view.output3.append("Plane" + getName() + " descending ...\n");
try { Thread.sleep (1000); }
catch ( InterruptedException e ) { }
view.output3.append("Plane" + getName() + " clearing runway and taxi to apron.\n\n");
}
//Landing completed
//Try to land on Runway 4
synchronized(view.output4) {
view.output4.append("Plane" + getName() + " approach runway.\n");
try { Thread.sleep (1000); }
catch ( InterruptedException e ) { }
view.output4.append("Plane" + getName() + " descending ...\n");
try { Thread.sleep (1000); }
catch ( InterruptedException e ) { }
view.output4.append("Plane" + getName() + " clearing runway and taxi to apron.\n\n");
}
//Landing completed
}
public void run()
{
Random rand = new Random();
int pickedNumber = rand.nextInt(10);
int updateInterval = 1000*pickedNumber;
try { Thread.sleep (updateInterval); }
catch ( InterruptedException e ) {}
view.status.append("Plane:" + getName() + " has entered the airspace.\n");
long start_time = System.currentTimeMillis();
land();
long end_time = System.currentTimeMillis();
long run_time = (end_time - start_time)/1000;
view.status.append("\tPlane" + getName() + " took " + run_time +" seconds to land.\n");
}
}
public class semaphores extends Frame implements ActionListener
{
private static final int WINDOW_HEIGHT = 700;
private static final int WINDOW_WIDTH = 1000;
private Label labelOne, labelTwo, runway1, runway2, runway3, runway4;
private TextField inputOne;
private Button runButton, quitButton;
public static TextArea status, output1, output2, output3, output4;
public semaphores()
{
setTitle("Airport Simulation");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setResizable(false);
setLocation(10,20);
setLayout(null);
labelOne = new Label("Number of Airplanes:");
labelOne.setBounds(5,17,140,40);
add(labelOne);
inputOne = new TextField();
inputOne.setBounds(150,25,135,25);
add(inputOne);
inputOne.addActionListener(this);
runButton = new Button("Run Program");
runButton.setBounds(5,60,100,25);
add(runButton);
runButton.addActionListener(this);
quitButton = new Button("Quit Program");
quitButton.setBounds(150,60,100,25);
add(quitButton);
quitButton.addActionListener(this);
labelTwo = new Label("Planes Status:");
labelTwo.setBounds(300,20,140,20);
add(labelTwo);
status = new TextArea(10,40);
status.setBounds(300,45,650, 180);
add(status);
runway1 = new Label("Runway 1:");
runway1.setBounds(10,240,140,20);
add(runway1);
output1 = new TextArea(10,40);
output1.setBounds(10,260,450, 200);
add(output1);
runway2 = new Label("Runway 2:");
runway2.setBounds(500,240,140,20);
add(runway2);
output2 = new TextArea(10,40);
output2.setBounds(500,260,450, 200);
add(output2);
runway3 = new Label("Runway 3:");
runway3.setBounds(10,470,140,20);
add(runway3);
output3 = new TextArea(10,40);
output3.setBounds(10,490,450, 200);
add(output3);
runway4 = new Label("Runway 4:");
runway4.setBounds(500,470,140,20);
add(runway4);
output4 = new TextArea(10,40);
output4.setBounds(500,490,450, 200);
add(output4);
}
public void actionPerformed(ActionEvent event)
{
int numThreads;
String getInput;
Button clickedButton = (Button) event.getSource();
if(clickedButton == runButton)
{
status.setText("");
output1.setText("");
output2.setText("");
output3.setText("");
output4.setText("");
getInput = inputOne.getText();
numThreads = Integer.parseInt(getInput);
thread_action[] threads = new thread_action[numThreads];
for(int x=0; x<numThreads; x++)
{
threads[x] = new thread_action(x, this);
}
for(int x=0; x<numThreads; x++)
{
threads[x].start();
}
}
else
{
System.exit(0);
}
}
public static void main(String[] args)
{
semaphores threadObj = new semaphores();
threadObj.setVisible(true);
}
}
import java.io.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.Random;
类thread\u操作扩展线程
{
私有信号量视图;
公共线程\u操作(int threadNumber,信号量视图)
{
超级(“+”螺纹编号);
this.view=视图;
}
公共土地()
{
//试着在1号跑道着陆
已同步(view.output1){
view.output1.append(“飞机”+getName()+“进近跑道”。\n”);
试试{Thread.sleep(2500);}
捕获(中断异常e){}
view.output1.append(“平面”+getName()+“降序…\n”);
试试{Thread.sleep(2500);}
捕获(中断异常e){}
view.output1.append(“飞机”+getName()+“清理跑道并滑行到停机坪。\n\n”);
}
//着陆完成
//试着在2号跑道着陆
已同步(view.output2){
view.output2.append(“飞机”+getName()+“进近跑道”。\n”);
试试{Thread.sleep(1000);}
捕获(中断异常e){}
view.output2.append(“平面”+getName()+“降序…\n”);
试试{Thread.sleep(1000);}
捕获(中断异常e){}
view.output2.append(“飞机”+getName()+“清理跑道并滑行到停机坪。\n\n”);
}
//着陆完成
//试着在3号跑道着陆
已同步(view.output3){
view.output3.append(“飞机”+getName()+“进近跑道”。\n”);
试试{Thread.sleep(1000);}
捕获(中断异常e){}
view.output3.append(“平面”+getName()+“降序…\n”);
试试{Thread.sleep(1000);}
捕获(中断异常e){}
view.output3.append(“飞机”+getName()+“清理跑道并滑行到停机坪。\n\n”);
}
//着陆完成
//试着在4号跑道着陆
已同步(view.output4){
view.output4.append(“飞机”+getName()+“进近跑道”。\n”);
试试{Thread.sleep(1000);}
捕获(中断异常e){}
view.output4.append(“平面”+getName()+“降序…\n”);
试试{Thread.sleep(1000);}
捕获(中断异常e){}
view.output4.append(“飞机”+getName()+“清理跑道并滑行到停机坪。\n\n”);
}
//着陆完成
}
公开募捐
{
Random rand=新的Random();
int pickedNumber=rand.nextInt(10);
int updateInterval=1000*pickedNumber;
请尝试{Thread.sleep(updateInterval);}
捕获(中断异常e){}
view.status.append(“飞机:“+getName()+”已进入空域。\n”);
长启动时间=System.currentTimeMillis();
土地();
long end_time=System.currentTimeMillis();
长期运行时间=(结束时间-开始时间)/1000;
view.status.append(“\tPlane”+getName()+”花了“+run_time+”秒时间着陆。\n”);
}
}
公共类信号量扩展框架实现ActionListener
{
专用静态最终int窗口_高度=700;
专用静态最终整数窗口_宽度=1000;
专用标签labelOne、labelTwo、跑道1、跑道2、跑道3、跑道4;
私有文本字段输入按钮;
私人按钮runButton,quitButton;
公共静态文本区状态,output1,output2,output3,output4;
公共信号量()
{
setTitle(“机场模拟”);
设置尺寸(窗宽、窗高);
可设置大小(假);
定位(10,20);
setLayout(空);
labelOne=新标签(“飞机数量:”);
拉贝隆.立根(5,17140,40);
添加(labelOne);
inputOne=新文本字段();
立根(150,25135,25);
添加(丁酮);
addActionListener(此);
runButton=新按钮(“运行程序”);
runButton.立根(5,60100,25);
添加(运行按钮);
runButton.addActionListener(这个);
quitButton=新按钮(“退出程序”);
1.立根(150,60100,25);
添加(退出按钮);
quitButton.addActionListener(此);
labelTwo=新标签(“飞机状态:”);
labelTwo.setBounds(300,20140,20);
添加(labelTwo);
状态=新文本区域(10,40);
地位.挫折(300,45650,180);
添加(状态);
跑道1=新标签(“跑道1:”);
跑道1.立根(10240140,20);
增加(跑道1);
output1=新文本区域(10,40);
输出1.立根(10260450200);
添加(输出1);
跑道2=新标签(“跑道2:”);
跑道2.立根(500240140,20);
增加(跑道2);
output2=新文本区域(10,40);
输出2.立根(500260450200);
添加(输出2);
跑道3=新标签(“跑道3:”);
跑道3.立根(10470140,20);
增加(跑道3);
output3=新文本区域(10,40);
输出3.挫折(10490450200);
添加(输出3);
跑道4=新标签(“跑道4:”);
跑道4.立根(500470140,20);
增加(跑道4);
output4=新文本区域(10,40);
输出4.立根(50049045200);
添加(输出4);
}
已执行的公共无效操作(操作事件)
{
int numThreads;
字符串输入;
Button clickedButton=(Button)event.getSource();
如果(单击按钮==运行按钮)
if (runway1available) {
synchronized(runway1) {
runway1available=false;
landonrunway1()
runway1available=true;
}
}
else if (runway2available) {
// ...
}