I want to create a multithread application, each thread represent a console and share common resources. Staring from a loop menu and a dummy resource
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;
}
}
Menu
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);
Is it possible to start 2 consoles that using the menu to access the same resource r ?