Skip to content

Commit a71e917

Browse files
committed
Thread Example Join, yield
1 parent 0fd9e37 commit a71e917

7 files changed

Lines changed: 169 additions & 2 deletions

File tree

core-java/src/com/coderbd/ex71/threads/ex2/Uncle.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ public void run() {
1515
if (i == 50) {
1616
break;
1717
}
18-
/* try {
18+
try {
19+
1920
Thread.sleep(1);
2021
} catch (InterruptedException ex) {
2122
Logger.getLogger(Uncle.class.getName()).log(Level.SEVERE, null, ex);
22-
}*/
23+
}
2324
}
2425
}
2526
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.coderbd.ex71.threads.ex3;
7+
8+
/**
9+
*
10+
* @author J2EE-33
11+
*/
12+
class MyRunnable implements Runnable{
13+
14+
@Override
15+
public void run() {
16+
System.out.println("Thread started:::"+Thread.currentThread().getName());
17+
try {
18+
Thread.sleep(4000);
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
System.out.println("Thread ended:::"+Thread.currentThread().getName());
23+
}
24+
25+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.coderbd.ex71.threads.ex3;
2+
3+
/**
4+
*
5+
* @author J2EE-33
6+
*/
7+
public class TestThreadJoin {
8+
9+
public static void main(String[] args) throws InterruptedException {
10+
Thread t1 = new Thread(new MyRunnable(), "t1");
11+
Thread t2 = new Thread(new MyRunnable(), "t2");
12+
Thread t3 = new Thread(new MyRunnable(), "t3");
13+
t1.start();
14+
15+
16+
//start second thread after waiting for 2 seconds or if it's dead
17+
try {
18+
t1.join(5000);
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
23+
t2.start();
24+
25+
//start third thread only when first thread is dead
26+
27+
28+
t3.start();
29+
30+
//let all threads finish execution before finishing main thread
31+
t1.join();
32+
t2.join();
33+
t3.join();
34+
35+
System.out.println("All threads are dead, exiting main thread");
36+
}
37+
38+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.coderbd.ex71.threads.ex4;
2+
/**
3+
*
4+
* @author J2EE-33
5+
*/
6+
public class App {
7+
public static void main(String[] args) {
8+
new TestThreadYield("Thread 1");
9+
new TestThreadYield("Thread 2");
10+
new TestThreadYield("Thread 3");
11+
}
12+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
*
3+
*
4+
* sleep() causes the thread to definitely stop executing for a given amount of
5+
* time; if no other thread or process needs to be run, the CPU will be idle
6+
* (and probably enter a power saving mode).
7+
*
8+
* yield() basically means that the thread is not doing anything particularly
9+
* important and if any other threads or processes need to be run, they should.
10+
* Otherwise, the current thread will continue to run.
11+
*
12+
*/
13+
package com.coderbd.ex71.threads.ex4;
14+
15+
/**
16+
*
17+
* @author J2EE-33
18+
*/
19+
public class TestThreadYield implements Runnable {
20+
Thread t;
21+
TestThreadYield(String str) {
22+
t = new Thread(this, str);
23+
// this will call run() function
24+
t.start();
25+
}
26+
@Override
27+
public void run() {
28+
for (int i = 0; i < 20; i++) {
29+
// yields control to another thread every 5 iterations
30+
if ((i % 5) == 0) {
31+
System.out.println(Thread.currentThread().getName()
32+
+ " yielding control...");
33+
/* causes the currently executing thread object to temporarily
34+
pause and allow other threads to execute */
35+
Thread.yield();
36+
}
37+
}
38+
System.out.println(Thread.currentThread().getName() + " has finished executing.");
39+
}
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.coderbd.ex71.threads.ex5;
2+
3+
import java.util.logging.Level;
4+
import java.util.logging.Logger;
5+
6+
/**
7+
*
8+
* @author J2EE-33
9+
*/
10+
public class SecondWay extends Thread {
11+
12+
int i = 0;
13+
14+
@Override
15+
public void run() {
16+
try {
17+
Thread.sleep(2000);
18+
} catch (InterruptedException ex) {
19+
Logger.getLogger(SecondWay.class.getName()).log(Level.SEVERE, null, ex);
20+
}
21+
22+
System.out.println("thread started running..");
23+
while (true) {
24+
System.out.println("Result of i: " + i++);
25+
if (i == 10) {
26+
break;
27+
}
28+
}
29+
30+
}
31+
32+
public static void main(String[] args) {
33+
Thread t = new SecondWay();
34+
t.start();
35+
}
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.coderbd.ex71.threads.ex6;
2+
3+
/**
4+
*
5+
* @author J2EE-33
6+
*/
7+
public class SimpleThread {
8+
9+
public static void main(String[] args) throws InterruptedException {
10+
Thread t = new Thread();
11+
t.start();
12+
t.sleep(20000);
13+
System.out.println("Thread Name: "+t.getName());
14+
}
15+
}

0 commit comments

Comments
 (0)