I had to create a multi-threading program.When the first thread completes and re
ID: 3805817 • Letter: I
Question
I had to create a multi-threading program.When the first thread completes and returns, I need to invoke System.exit() to terminate the program; in so doing, I will be able to determine which thread "won" and achieved it's conclusion first.
Here is the code I have so far, all threads complete. How do I invoke that method to terminate the program when the first thread reaches 10?
public class Assignment {
public static void main(String[] args) {
Runnable runnable1 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 1: "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Error in Thread 1: "+e.getMessage());
}
}
};
Runnable runnable2 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 2: "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Error in Thread 2: "+e.getMessage());
}
}
};
Runnable runnable3 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 3: "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Error in Thread 3: "+e.getMessage());
}
}
};
Runnable runnable4 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 4: "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Error in Thread 4: "+e.getMessage());
}
}
};
Runnable runnable5 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 5: "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Error in Thread 5: "+e.getMessage());
}
}
};
Runnable runnable6 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 6: "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Error in Thread 6: "+e.getMessage());
}
}
};
Runnable runnable7 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 7: "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Error in Thread 7: "+e.getMessage());
}
}
};
Runnable runnable8 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 8: "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Error in Thread 8: "+e.getMessage());
}
}
};
Runnable runnable9 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 9: "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Error in Thread 9: "+e.getMessage());
}
}
};
Runnable runnable10 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 10: "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Error in Thread 10: "+e.getMessage());
}
}
};
Thread thread1 = new Thread(runnable1);
Thread thread2 = new Thread(runnable2);
Thread thread3 = new Thread(runnable3);
Thread thread4 = new Thread(runnable4);
Thread thread5 = new Thread(runnable5);
Thread thread6 = new Thread(runnable6);
Thread thread7 = new Thread(runnable7);
Thread thread8 = new Thread(runnable8);
Thread thread9 = new Thread(runnable9);
Thread thread10 = new Thread(runnable10);
System.out.println("Thread Start: ");
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread6.start();
thread7.start();
thread8.start();
thread9.start();
thread10.start();
}
}
Explanation / Answer
here i submitted the code without compile beacuse my compiler does'nt working right now so may be you got a bug...if you any bug found please feel free to ask in comment section.
public class Assignment {
public static void main(String[] args) {
Runnable runnable1 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 1: "+i);
try {
Thread.sleep(1000);
if(i==10)
{
System.Exit();
}else{
System.out.println("thread 1 is running");
}
} catch (InterruptedException e) {
System.out.println("Error in Thread 1: "+e.getMessage());
}
}
};
Runnable runnable2 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 2: "+i);
try {
Thread.sleep(1000);
if(i==10)
{
System.Exit();
}else{
System.out.println("thread 2 is running");
}
} catch (InterruptedException e) {
System.out.println("Error in Thread 2: "+e.getMessage());
}
}
};
Runnable runnable3 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 3: "+i);
try {
Thread.sleep(1000);
if(i==10)
{
System.Exit();
}else{
System.out.println("thread 3 is running");
}
} catch (InterruptedException e) {
System.out.println("Error in Thread 3: "+e.getMessage());
}
}
};
Runnable runnable4 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 4: "+i);
try {
Thread.sleep(1000);
if(i==10)
{
System.Exit();
}else{
System.out.println("thread 4 is running");
}
} catch (InterruptedException e) {
System.out.println("Error in Thread 4: "+e.getMessage());
}
}
};
Runnable runnable5 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 5: "+i);
try {
Thread.sleep(1000);
if(i==10)
{
System.Exit();
}else{
System.out.println("thread 5 is running");
}
} catch (InterruptedException e) {
System.out.println("Error in Thread 5: "+e.getMessage());
}
}
};
Runnable runnable6 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 6: "+i);
try {
Thread.sleep(1000);
if(i==10)
{
System.Exit();
}else{
System.out.println("thread 6 is running");
}
} catch (InterruptedException e) {
System.out.println("Error in Thread 6: "+e.getMessage());
}
}
};
Runnable runnable7 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 7: "+i);
try {
Thread.sleep(1000);
if(i==10)
{
System.Exit();
}else{
System.out.println("thread 7 is running");
}
} catch (InterruptedException e) {
System.out.println("Error in Thread 7: "+e.getMessage());
}
}
};
Runnable runnable8 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 8: "+i);
try {
Thread.sleep(1000);
if(i==10)
{
System.Exit();
}else{
System.out.println("thread 8 is running");
}
} catch (InterruptedException e) {
System.out.println("Error in Thread 8: "+e.getMessage());
}
}
};
Runnable runnable9 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 9: "+i);
try {
Thread.sleep(1000);
if(i==10)
{
System.Exit();
}else{
System.out.println("thread 9 is running");
}
} catch (InterruptedException e) {
System.out.println("Error in Thread 9: "+e.getMessage());
}
}
};
Runnable runnable10 = () -> {
for(int i = 1; i <=10; i++){
System.out.println("Thread 10: "+i);
try {
Thread.sleep(1000);
if(i==10)
{
System.Exit();
}else{
System.out.println("thread 10 is running");
}
} catch (InterruptedException e) {
System.out.println("Error in Thread 10: "+e.getMessage());
}
}
};
Thread thread1 = new Thread(runnable1);
Thread thread2 = new Thread(runnable2);
Thread thread3 = new Thread(runnable3);
Thread thread4 = new Thread(runnable4);
Thread thread5 = new Thread(runnable5);
Thread thread6 = new Thread(runnable6);
Thread thread7 = new Thread(runnable7);
Thread thread8 = new Thread(runnable8);
Thread thread9 = new Thread(runnable9);
Thread thread10 = new Thread(runnable10);
System.out.println("Thread Start: ");
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread6.start();
thread7.start();
thread8.start();
thread9.start();
thread10.start();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.