Computer Science, asked by xiimia3sma30, 1 year ago

Make a program use the multithread concept with the following conditions [In JAVA PROGRAMMING LANGUAGE] :
1. The application runs 3 threads with names 1, 2, and 3
2. All threads have an initial value of 0
3. Applications randomize integers 1-3
      a. If the number appears 1, then the value
          thread 1 increases 1
      b. If the number appears 2, then the value
          thread 2 increases 1
      c. If the number appears 3, then the value
          thread 3 increases 1
4. The thread that reaches number 10 is the winner, and the application is complete.

IN JAVA PROGRAMMING LANGUAGES !!!

Answers

Answered by Inspectator
1

Answer:

Example 1:

class MultithreadingDemo extends Thread{

public void run(){

System.out.println("My thread is in running state.");

}

public static void main(String args[]){

MultithreadingDemo obj=new MultithreadingDemo();

obj.start();

}

}

Output:

My thread is in running state.

Example 2:

class Count extends Thread

{

Count()

{

super("my extending thread");

System.out.println("my thread created" + this);

start();

}

public void run()

{

try

{

for (int i=0 ;i<10;i++)

{

System.out.println("Printing the count " + i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println("my thread interrupted");

}

System.out.println("My thread run is over" );

}

}

class ExtendingExample

{

public static void main(String args[])

{

Count cnt = new Count();

try

{

while(cnt.isAlive())

{

System.out.println("Main thread will be alive till the child thread is live");

Thread.sleep(1500);

}

}

catch(InterruptedException e)

{

System.out.println("Main thread interrupted");

}

System.out.println("Main thread's run is over" );

}

}

Output:

my thread createdThread[my runnable thread,5,main]

Main thread will be alive till the child thread is live

Printing the count 0

Printing the count 1

Main thread will be alive till the child thread is live

Printing the count 2

Main thread will be alive till the child thread is live

Printing the count 3

Printing the count 4

Main thread will be alive till the child thread is live

Printing the count 5

Main thread will be alive till the child thread is live

Printing the count 6

Printing the count 7

Main thread will be alive till the child thread is live

Printing the count 8

Main thread will be alive till the child thread is live

Printing the count 9

mythread run is over

Main thread run is over

Similar questions