Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider a system where many players log in to play a game. A new thread is crea

ID: 3877873 • Letter: C

Question

Consider a system where many players log in to play a game. A new thread is created for each player that joins the system and the thread is given an ID. After the players log in, they need to form into teams of four threads. To do this, the threads call the method

int [ ] makeTeam( int my ID )

which returns an integer array giving the ID numbers of all four players on the team. When the first thread calls makeTeam, it does not know who the other three players will be. It must wait until the other three players call makeTeam before it can receive the array of player ids. The second and third players will also have to wait.

1. Write the makeTeam method using any programming language and any concurrency paradigm.

Explanation / Answer

Team class:

public class Team {

   private int id[];
   private static int count=1;
   Team(){
       id=new int[4];
              
   }
  
   synchronized int [ ] makeTeam( int ID ){
      
       if(count<=3){
           id[count]=ID;
           System.out.println("Add more players . Current players:"+(count+1));
           count++;
           try{
               wait();
           }
           catch(Exception e){
               System.out.println(e.getMessage());
           }
       }
       else
       {
           System.out.println("Total no of players are 4 .You can start the game");
           return id;
       }
       return id;
   }

   public int[] getId() {
       return id;
   }

   public void setId(int[] id) {
       this.id = id;
   }

   public static int getCount() {
       return count;
   }

   public static void setCount(int count) {
       Team.count = count;
   }
  
  
}

Therea class:


class ThreadDemo extends Thread {
private Thread t;
private String threadName;
Team tm;
private int idno;

ThreadDemo(String name, int idno) {
threadName = name;
tm = new Team();
this.idno=idno;
}

public void run() {
  
synchronized(tm) {
int []a=tm.makeTeam(idno);
}
System.out.println("Thread " + threadName + " exiting.");
}

public void start () {
System.out.println("Starting " + threadName );
  
if (t == null) {
t = new Thread (this, threadName);
t.start ();
}
}
}

Main:

public class Test {

   public static void main(String[] args) {

   ThreadDemo t1=new ThreadDemo("1", 12);
       ThreadDemo t2=new ThreadDemo("2", 121);
       ThreadDemo t3=new ThreadDemo("3", 1233);
       ThreadDemo t4=new ThreadDemo("4", 1244);
       ThreadDemo t5=new ThreadDemo("4", 1244);
       t1.start();
       t2.start();
       t3.start();
       t4.start();

}

}

Output:

Starting 1
Starting 2
Starting 3
Starting 4
Add more players . Current players:2
Add more players . Current players:3
Add more players . Current players:3
Total no of players are 4 .You can start the game

note: feel free to drop comments in case any concern