【发布时间】:2020-11-03 12:50:06
【问题描述】:
我想创建一个多线程应用程序,每个线程代表一个控制台并共享公共资源。从循环菜单和虚拟资源开始
public class Resource {
int id;
public Resource(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
菜单
int choiceentry;
Scanner scanchoice = new Scanner(System.in);
Resource r = new Resource(10);
//create 2 threads both having a console with menu and acces to the same resource r
do {
System.out.print("1.Get resource\n2.Set resource to random\n\n");
System.out.print("Enter your option: ");
choiceentry = scanchoice.nextInt();
switch (choiceentry) {
//get the resource id
case 1:
System.out.println("Resource get: " + r.getId());
break;
//set the resource id
case 2:
Random rand = new Random();
int newId = rand.nextInt(10);
r.setId(newId);
System.out.println("Resource set to: " + newId);
break;
case 4:
System.exit(0);
default:
System.out.println("Choice must be a value between 1 and 3.");
}
System.out.print("\n\n");
} while (true);
是否可以启动 2 个使用菜单访问相同资源的控制台 r ?
【问题讨论】:
-
“两个控制台”是什么意思?两个黑框一个命令提示符在里面吗?
-
命令提示符
-
单个进程有一个标准输入/标准输出。所以不是真的,不。 GUI 将为多个输入提供更容易的访问。还有see here.
标签: java multithreading eclipse