Last active
September 24, 2020 05:39
-
-
Save sne007/28546c721e444743ac2d0f04fa94d931 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tasks { | |
public static void main(String[] args) { | |
Tasks t = new Tasks(); | |
Task1 t1 = new Task1(); | |
Task2 t2 = new Task2(); | |
Task3 t3 = new Task3(); | |
t1.start(); | |
t2.start(); | |
t3.start(); | |
} | |
} | |
class Task1 extends Thread { | |
public void run() { | |
System.out.println("T1 is running on thread " + Thread.currentThread().getName()); | |
int n = computePrimeNumber(); | |
putPrimeNumberInDatabase(n); | |
} | |
public int computePrimeNumber() { | |
System.out.println("Computed prime number 17"); | |
return 17; | |
} | |
public void putPrimeNumberInDatabase(int n) { | |
try { | |
Thread.sleep(5000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("Successfully put " + n + " in the database"); | |
} | |
} | |
class Task2 extends Thread { | |
public void run() { | |
System.out.println("T2 is running on thread " + Thread.currentThread().getName()); | |
int n = computePrimeNumber(); | |
putPrimeNumberInDatabase(n); | |
} | |
public int computePrimeNumber() { | |
System.out.println("Computed prime number 23"); | |
return 23; | |
} | |
public void putPrimeNumberInDatabase(int n) { | |
try { | |
Thread.sleep(5000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("Successfully put " + n + " in the database"); | |
} | |
} | |
class Task3 extends Thread { | |
public void run() { | |
System.out.println("T3 is running on thread " + Thread.currentThread().getName()); | |
int n = computePrimeNumber(); | |
putPrimeNumberInDatabase(n); | |
} | |
public int computePrimeNumber() { | |
System.out.println("Computed prime number 13"); | |
return 13; | |
} | |
public void putPrimeNumberInDatabase(int n) { | |
try { | |
Thread.sleep(5000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("Successfully put " + n + " in the database"); | |
} | |
} | |
/* | |
Output: | |
T1 is running on thread Thread-0 | |
T3 is running on thread Thread-2 | |
T2 is running on thread Thread-1 | |
Computed prime number 13 | |
Computed prime number 17 | |
Computed prime number 23 | |
Successfully put 17 in the database | |
Successfully put 23 in the database | |
Successfully put 13 in the database | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment