Skip to content

Commit b2fda15

Browse files
committed
Multithreading programs
1 parent 339b844 commit b2fda15

11 files changed

+524
-3
lines changed

HelloWorld.java

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
public class HelloWorld {
2-
public static void main() {
3-
System.out.println("HelloWorld");
2+
protected int x;
3+
private int y;
4+
static int z;
5+
6+
HelloWorld() {
7+
x = 10;
8+
y = 20;
9+
z = 30;
10+
}
11+
12+
HelloWorld(int a, int y, int z) {
13+
x = a;
14+
this.y = y;
15+
this.z = z;
16+
}
17+
18+
void show() {
19+
int x = 100;
20+
int a = 200;
21+
System.out.println(x + " " + y + " " + z);
22+
System.out.println(this.x + " " + y + " " + z);
23+
// System.out.println(this.a);
24+
}
25+
}
26+
27+
class ABC {
28+
public static void main(String[] args) {
29+
HelloWorld world = new HelloWorld();
30+
world.show();
431
}
532
}

MethodOvrloading.java

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class MethodOverload {
22
// Method Overloading via fun()
33
void fun() { // Function 1
4+
int x = 5;
45
System.out.println("I am a function with no args");
56
}
67

Multithreading.java

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.util.Scanner;
2+
3+
class MyThread1 extends Thread {
4+
@Override
5+
public void run() {
6+
// System.out.println("MyThread1 run() function");
7+
8+
// Scanner sc = new Scanner(System.in);
9+
// int x = sc.nextInt(); // Blocking I/O operation
10+
11+
for (int i = 0; i < 1; i++) {
12+
System.out.println("MyThread1 run() function");
13+
}
14+
}
15+
}
16+
17+
class MyThread2 implements Runnable {
18+
public void run() {
19+
Thread.currentThread().setPriority(6);
20+
System.out.println(Thread.currentThread().getPriority());
21+
Thread.currentThread().setPriority(7);
22+
System.out.println(Thread.currentThread().getPriority());
23+
for (int i = 0; i < 3; i++) {
24+
System.out.println("MyThread2 run() function");
25+
}
26+
Thread.currentThread().setPriority(2);
27+
System.out.println("MyThread2 run() function");
28+
}
29+
}
30+
31+
public class Multithreading {
32+
public static void main(String[] args) {
33+
MyThread1 t1 = new MyThread1(); // Newborn state
34+
t1.setPriority(2);
35+
t1.start(); // Ready state
36+
37+
// Scanner sc = new Scanner(System.in);
38+
// int x = sc.nextInt(); // Blocking I/O operation
39+
40+
MyThread2 t = new MyThread2();
41+
Thread t2 = new Thread(t); // Newborn state
42+
t2.setPriority(8);
43+
t2.start(); // Ready state
44+
45+
System.out.println(t1.getName());
46+
System.out.println(t2.getName());
47+
48+
t1.setName("Thread of MyThread1");
49+
t2.setName("Thread of MyThread2");
50+
51+
System.out.println(t1.getName());
52+
System.out.println(t2.getName());
53+
System.out.println(Thread.currentThread().getName());
54+
55+
System.out.println(t1.getId());
56+
System.out.println(t2.getId());
57+
System.out.println(Thread.currentThread().getId());
58+
59+
// try {
60+
// Thread.sleep(10);
61+
// t1.join();
62+
// t2.join(1000);
63+
// } catch (InterruptedException e) {
64+
// System.out.println(e);
65+
// }
66+
67+
System.out.println("Main function terminating"); // Blocking I/O operation
68+
}
69+
}

NestedClasses.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ public static void main(String[] args) {
6060
OuterClass.MemberInnerClass.memberStaticInnerFun();
6161
memberInner.memberStaticInnerFun();
6262
}
63-
}
63+
}

ReentrantMonitors.java

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class Warehouse {
2+
int items = 4;
3+
4+
synchronized int increment(int x) {
5+
System.out.println("Incremented to=>" + (items + x));
6+
items += x;
7+
return decrement(1);
8+
}
9+
10+
synchronized int decrement(int x) {
11+
if (items >= x) {
12+
System.out.println("Decremented to=>" + (items - x));
13+
items -= x;
14+
return items;
15+
}
16+
return 0;
17+
}
18+
19+
void display() {
20+
System.out.println("Total items in warehouse =>" + items);
21+
}
22+
23+
}
24+
25+
class Thread5 extends Thread {
26+
Warehouse warehouse;
27+
28+
public Thread5(Warehouse warehouse) {
29+
this.warehouse = warehouse;
30+
}
31+
32+
@Override
33+
public void run() {
34+
warehouse.increment(3);
35+
}
36+
}
37+
38+
class Thread6 extends Thread {
39+
Warehouse warehouse;
40+
41+
public Thread6(Warehouse warehouse) {
42+
this.warehouse = warehouse;
43+
}
44+
45+
@Override
46+
public void run() {
47+
warehouse.decrement(2);
48+
}
49+
}
50+
51+
public class ReentrantMonitors {
52+
public static void main(String[] args) throws InterruptedException {
53+
Warehouse warehouse = new Warehouse();
54+
Thread5 thread5 = new Thread5(warehouse);
55+
Thread6 thread6 = new Thread6(warehouse);
56+
57+
thread5.start();
58+
thread6.start();
59+
thread5.join();
60+
thread6.join();
61+
warehouse.display();
62+
}
63+
}

ReflectMemberInnerClass.java

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class ReflectMemberInnerClass {
2+
private int x = 10;
3+
4+
class InnerClass {
5+
6+
}
7+
}

SynchronizedMethodAbhishek

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package MutliThreading;
2+
3+
class Warehouse {
4+
static int items = 4;
5+
6+
private int increment(int x) {
7+
System.out.println("Incremented to=>" + (items + x));
8+
items += x;
9+
return items;
10+
}
11+
12+
private int decrement(int x) {
13+
if (items >= x) {
14+
System.out.println("Decremented to=>" + (items - x));
15+
items -= x;
16+
return items;
17+
}
18+
return 0;
19+
}
20+
21+
private void display() {
22+
System.out.println("Total items in warehouse =>" + items);
23+
}
24+
25+
synchronized void change(int x,int y) {
26+
this.increment(x);
27+
this.decrement(y);
28+
this.display();
29+
30+
}
31+
32+
}
33+
34+
class Thread5 extends Thread {
35+
Warehouse warehouse;
36+
37+
public Thread5(Warehouse warehouse) {
38+
this.warehouse = warehouse;
39+
}
40+
41+
@Override
42+
public void run() {
43+
warehouse.change(5,3);
44+
}
45+
}
46+
47+
class Thread6 extends Thread {
48+
Warehouse warehouse;
49+
50+
public Thread6(Warehouse warehouse) {
51+
this.warehouse = warehouse;
52+
}
53+
54+
@Override
55+
synchronized public void run() {
56+
warehouse.change(2,4);
57+
}
58+
}
59+
60+
public class SynchronizedMethod2 {
61+
public static void main(String[] args) throws InterruptedException {
62+
Warehouse warehouse = new Warehouse();
63+
Thread5 thread5 = new Thread5(warehouse);
64+
Thread6 thread6 = new Thread6(warehouse);
65+
66+
thread5.start();
67+
thread6.start();
68+
thread6.join();
69+
System.out.println("Finally we have " + Warehouse.items + " items in the warehouse");
70+
}
71+
}

TestThreadCoopearation.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Customer {
2+
private int amount = 10000;
3+
4+
synchronized void withdraw(int amount) {
5+
System.out.println("going to withdraw...");
6+
while (this.amount < amount) {
7+
System.out.println("Less balance; waiting for deposit...");
8+
try {
9+
wait();
10+
} catch (Exception e) {
11+
}
12+
}
13+
this.amount -= amount;
14+
System.out.println("withdraw completed...");
15+
}
16+
17+
synchronized void deposit(int amount) {
18+
System.out.println("going to deposit...");
19+
this.amount += amount;
20+
System.out.println("deposit completed... ");
21+
notifyAll();
22+
}
23+
}
24+
25+
class TestThreadCooperation {
26+
public static void main(String args[]) {
27+
final Customer c = new Customer();
28+
new Thread() {
29+
public void run() {
30+
c.withdraw(15000);
31+
}
32+
}.start(); // Thread-1
33+
34+
new Thread() {
35+
public void run() {
36+
c.withdraw(11000);
37+
}
38+
}.start(); // Thread-1
39+
40+
new Thread() {
41+
public void run() {
42+
c.withdraw(10000);
43+
}
44+
}.start(); // Thread-1
45+
46+
new Thread() {
47+
public void run() {
48+
c.deposit(1000);
49+
}
50+
}.start(); // Thread-2
51+
}
52+
}

ThreadDeadlock.java

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
public class ThreadDeadlock {
2+
3+
static boolean flag1 = false;
4+
static boolean flag2 = false;
5+
6+
public static void main(String[] args) {
7+
final String resource1 = "ratan tata";
8+
final String resource2 = "mukesh ambani";
9+
10+
// t1 tries to lock resource1 then resource2
11+
Runnable t = () -> {
12+
synchronized (resource1) {
13+
ThreadDeadlock.flag1 = true;
14+
System.out.println("Thread 1: locked resource 1");
15+
try {
16+
Thread.sleep(100);
17+
} catch (Exception e) {
18+
}
19+
ThreadDeadlock.flag1 = false;
20+
resource1.notify();
21+
}
22+
23+
try {
24+
while (ThreadDeadlock.flag2) {
25+
resource2.wait();
26+
}
27+
} catch (InterruptedException e) {
28+
// TODO Auto-generated catch block
29+
e.printStackTrace();
30+
}
31+
synchronized (resource2) {
32+
ThreadDeadlock.flag2 = true;
33+
System.out.println("Thread 1: locked resource 2");
34+
ThreadDeadlock.flag2 = false;
35+
resource2.notify();
36+
}
37+
};
38+
Thread t1 = new Thread(t);
39+
40+
// t2 tries to lock resource2 then resource1
41+
Thread t2 = new Thread() {
42+
@Override
43+
public void run() {
44+
synchronized (resource2) {
45+
ThreadDeadlock.flag2 = true;
46+
System.out.println("Thread 2: locked resource 2");
47+
try {
48+
Thread.sleep(100);
49+
} catch (Exception e) {
50+
}
51+
ThreadDeadlock.flag2 = false;
52+
resource2.notify();
53+
}
54+
55+
try {
56+
while (ThreadDeadlock.flag1) {
57+
resource1.wait();
58+
}
59+
} catch (InterruptedException e) {
60+
// TODO Auto-generated catch block
61+
e.printStackTrace();
62+
}
63+
synchronized (resource1) {
64+
ThreadDeadlock.flag1 = true;
65+
System.out.println("Thread 2: locked resource 1");
66+
ThreadDeadlock.flag1 = false;
67+
resource1.notify();
68+
}
69+
}
70+
};
71+
72+
t1.start();
73+
t2.start();
74+
}
75+
}

0 commit comments

Comments
 (0)