Skip to content

Commit 647c1dd

Browse files
Add files via upload
1 parent 9cdbec7 commit 647c1dd

28 files changed

+243
-0
lines changed
1.3 KB
Binary file not shown.
724 Bytes
Binary file not shown.
1.14 KB
Binary file not shown.
1.29 KB
Binary file not shown.
933 Bytes
Binary file not shown.
669 Bytes
Binary file not shown.
543 Bytes
Binary file not shown.
543 Bytes
Binary file not shown.
Binary file not shown.
1.07 KB
Binary file not shown.
2.62 KB
Binary file not shown.
2.21 KB
Binary file not shown.
2.01 KB
Binary file not shown.

Class16/bin/module-info.class

146 Bytes
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.david.class161;
2+
3+
public class Example1 {
4+
public static void main(String[] args) {
5+
Runnable r = ()-> System.out.println("Thread started");
6+
Thread t1 = new Thread(r);
7+
t1.start();
8+
Thread t2 = new Thread(r);
9+
t2.start();
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.david.class161;
2+
3+
public class MyThread extends Thread{
4+
@Override
5+
public void run() {
6+
System.out.println("Thread started");
7+
}
8+
public static void main(String[] args) {
9+
MyThread t1 = new MyThread();
10+
t1.start();
11+
MyThread t2 = new MyThread();
12+
t2.start();
13+
}
14+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.david.class162;
2+
3+
public class JoinThread extends Thread{
4+
@Override
5+
public void run() {
6+
System.out.println("Thread started");
7+
for(int i =1; i<5;i++) {
8+
try {
9+
Thread.sleep(500);
10+
} catch (InterruptedException e) {
11+
e.printStackTrace();
12+
}
13+
System.out.println(i);
14+
}
15+
System.out.println("Thread ended");
16+
}
17+
18+
public static void main(String[] args) {
19+
JoinThread t1 = new JoinThread();
20+
JoinThread t2 = new JoinThread();
21+
JoinThread t3 = new JoinThread();
22+
t1.start();
23+
try {
24+
t1.join();
25+
} catch (InterruptedException e) {
26+
e.printStackTrace();
27+
}
28+
t2.start();
29+
t3.start();
30+
31+
}
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.david.class162;
2+
3+
public class MyThread extends Thread{
4+
@Override
5+
public void run() {
6+
System.out.println("MyThread Started");
7+
}
8+
9+
public static void main(String[] args) {
10+
MyThread m = new MyThread();
11+
System.out.println("id : "+m.getId());
12+
System.out.println("name : "+m.getName());
13+
System.out.println("priority : "+m.getPriority());
14+
System.out.println("Current thread name : "+currentThread().getName());
15+
System.out.println("is Daemon : "+m.isDaemon());
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.david.class163;
2+
3+
public class Counter {
4+
synchronized void count(int n){
5+
for(int i =1;i<n;i++){
6+
System.out.println("i = "+i);
7+
try {
8+
Thread.sleep(100);
9+
}catch(Exception e){
10+
System.out.println(e);
11+
}
12+
}
13+
14+
15+
}
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.david.class163;
2+
3+
public class Example1 {
4+
public static void main(String[] args) {
5+
Counter c= new Counter();
6+
new MyThread1(c).start();
7+
new MyThread2(c).start();
8+
}
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.david.class163;
2+
3+
public class MyThread1 extends Thread{
4+
Counter counter;
5+
public MyThread1(Counter c) {
6+
this.counter = c;
7+
}
8+
@Override
9+
public void run() {
10+
counter.count(10);
11+
}
12+
13+
14+
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.david.class163;
2+
3+
public class MyThread2 extends Thread{
4+
Counter counter;
5+
public MyThread2(Counter c) {
6+
this.counter = c;
7+
}
8+
@Override
9+
public void run() {
10+
counter.count(20);
11+
}
12+
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.david.class164;
2+
3+
public class EmployeeThread implements Runnable{
4+
private String name;
5+
public EmployeeThread(String name) {
6+
this.name =name;
7+
}
8+
@Override
9+
public void run() {
10+
System.out.println(" Thread "+Thread.currentThread().getId()+" started");
11+
System.out.println("name = "+ this.name);;
12+
try {
13+
Thread.sleep(100);
14+
} catch (InterruptedException e) {
15+
e.printStackTrace();
16+
}
17+
}
18+
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.david.class164;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
public class Example1 {
7+
public static void main(String[] args) {
8+
ExecutorService executor = Executors.newFixedThreadPool(3);
9+
String[] names = {"David","Paul","Sam","Adam", "Ben"};
10+
for(String name : names) {
11+
Runnable eThread = new EmployeeThread(name);
12+
executor.execute(eThread);
13+
}
14+
executor.shutdown();
15+
}
16+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.david.class165;
2+
3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.Future;
6+
import java.util.concurrent.ThreadPoolExecutor;
7+
8+
public class Example1 {
9+
public static void main(String[] args) {
10+
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
11+
Future<String> s1 = executor.submit(() -> {
12+
System.out.println("Thread1 started");
13+
Thread.sleep(500);
14+
return "task1 done";
15+
});
16+
Future<String> s2 = executor.submit(() -> {
17+
System.out.println("Thread2 started");
18+
Thread.sleep(500);
19+
return "task2 done";
20+
});
21+
System.out.println("Pool size : "+executor.getPoolSize());
22+
System.out.println("Queue size : "+executor.getQueue().size());
23+
try {
24+
System.out.println(s1.get());
25+
System.out.println(s2.get());
26+
} catch (InterruptedException | ExecutionException e) {
27+
e.printStackTrace();
28+
}
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.david.class165;
2+
3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.Future;
6+
import java.util.concurrent.ThreadPoolExecutor;
7+
8+
public class Example2 {
9+
public static void main(String[] args) {
10+
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
11+
Future<String> op1 = executor.submit(() -> {
12+
Thread.sleep(500);
13+
return "thread 1 done";
14+
});
15+
Future<String> op2 = executor.submit(() -> {
16+
Thread.sleep(500);
17+
return "thread 2 done";
18+
});
19+
try {
20+
System.out.println(op1.get());
21+
System.out.println(op2.get());
22+
} catch (InterruptedException | ExecutionException e) {
23+
e.printStackTrace();
24+
}
25+
26+
}
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.david.class165;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.atomic.AtomicInteger;
6+
7+
public class Example3 {
8+
public static void main(String[] args) {
9+
AtomicInteger counter = new AtomicInteger();
10+
11+
ExecutorService executor = Executors.newSingleThreadExecutor();
12+
executor.submit(() -> {
13+
counter.set(1);
14+
System.out.println("counter 1 :"+counter.get());
15+
});
16+
executor.submit(() -> {
17+
counter.compareAndSet(1, 2);
18+
System.out.println("counter 2 : "+counter.get());
19+
});
20+
21+
}
22+
}

Class16/src/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module Class16 {
2+
}

0 commit comments

Comments
 (0)