Skip to content

Commit da72f86

Browse files
committed
MultiThread--Interthread
1 parent 69b2b7a commit da72f86

12 files changed

Lines changed: 673 additions & 0 deletions
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package practice;
2+
class ChildThread145 implements Runnable{
3+
Thread child;
4+
ChildThread145(String name)
5+
{
6+
child=new Thread(this,name);
7+
child.start();
8+
}
9+
public void run()
10+
{
11+
System.out.println("Start Child Thread");
12+
for(int i=0;i<5;i++)
13+
{
14+
try {
15+
System.out.println(Thread.currentThread().getName()+" : "+i);
16+
System.out.println("Sleeping mode is open");
17+
Thread.sleep(500);
18+
System.out.println("Wokeup Thread");
19+
}catch(InterruptedException k)
20+
{
21+
System.out.println(k);
22+
}
23+
}
24+
System.out.println("Child Thread ends");
25+
}
26+
}
27+
28+
public class ThreadDemo14 {
29+
30+
public static void main(String[]args)
31+
{
32+
ChildThread145 t=new ChildThread145("Keshav");
33+
ChildThread145 t1=new ChildThread145("Ramdev");
34+
ChildThread145 t2=new ChildThread145("MAhadev");
35+
36+
System.out.println("Start main Thread");
37+
for(int i=0;i<5;i++)
38+
{
39+
try {
40+
System.out.println("Main thread : "+i);
41+
System.out.println("Sleeping mode is open");
42+
Thread.sleep(500);
43+
System.out.println("Wokeup Thread");
44+
}catch(InterruptedException k)
45+
{
46+
System.out.println(k);
47+
}
48+
}
49+
System.out.println("main Thread ends");
50+
51+
}
52+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package practice;
2+
class ChildThread165 implements Runnable{
3+
Thread child;
4+
ChildThread165(String name)
5+
{
6+
child=new Thread(this,name);
7+
child.start();
8+
child.setPriority(2);
9+
}
10+
public void run()
11+
{
12+
System.out.println("Starting child Thread");
13+
for(int i=0;i<5;i++)
14+
{
15+
try {
16+
System.out.println(Thread.currentThread().getName()+":"+i);
17+
System.out.println("Thraed is sleeping");
18+
Thread.sleep(500);
19+
System.out.println("Thread is on");
20+
}catch(InterruptedException k)
21+
{
22+
System.out.println(k);
23+
}
24+
}
25+
System.out.println("Child is end now");
26+
}
27+
28+
}
29+
public class ThreadDemo15 {
30+
public static void main(String[]args)
31+
{
32+
ChildThread165 c=new ChildThread165("Hitesh");
33+
ChildThread165 c1=new ChildThread165("Ashok");
34+
ChildThread165 c2=new ChildThread165("Sonar");
35+
36+
System.out.println("Thread is alive or not");
37+
System.out.println(c.child.getName()+"Alive ?"+c.child.isAlive());
38+
System.out.println(c1.child.getName()+"Alive ?"+c1.child.isAlive());
39+
System.out.println(c2.child.getName()+"Alive ?"+c2.child.isAlive());
40+
41+
try {
42+
for(int i=5;i>0;i--)
43+
{
44+
System.out.println("main thread :"+i);
45+
46+
c.child.join();
47+
c1.child.join();
48+
c2.child.join();
49+
50+
System.out.println(c.child.getName()+"Alive ?"+c.child.isAlive());
51+
System.out.println(c1.child.getName()+"Alive ?"+c1.child.isAlive());
52+
System.out.println(c2.child.getName()+"Alive ?"+c2.child.isAlive());
53+
}
54+
}catch(InterruptedException k)
55+
{
56+
System.out.println(k);
57+
}
58+
System.out.println("main thread shut down");
59+
}
60+
61+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package practice;
2+
3+
import java.util.Scanner;
4+
class HusbendThread implements Runnable{
5+
Thread hchild;
6+
Bank12 b;
7+
HusbendThread(Bank12 b)
8+
{
9+
this.b=b;
10+
hchild=new Thread(this,"HusbendThread");
11+
12+
hchild.start();
13+
}
14+
public void run()
15+
{
16+
b.deposite();
17+
}
18+
}
19+
class WifeThread implements Runnable{
20+
Thread wchild;
21+
Bank12 b;
22+
WifeThread(Bank12 b)
23+
{
24+
this.b=b;
25+
wchild=new Thread(this,"WifeThread");
26+
27+
wchild.start();
28+
}
29+
public void run()
30+
{
31+
b.withdraw();
32+
}
33+
}//class WifeThread implements Runnable{
34+
35+
36+
37+
class Bank12{
38+
volatile int balance=5000;
39+
private int deposite;
40+
private int withdraw;
41+
Scanner sc=new Scanner(System.in);
42+
public void deposite()
43+
{ try {
44+
System.out.println("Balance before deposite : "+balance);
45+
Thread.sleep(1000);
46+
System.out.println("Enter the deposite ammount");
47+
deposite=sc.nextInt();
48+
Thread.sleep(500);
49+
50+
balance=balance+deposite;
51+
Thread.sleep(1500);
52+
System.out.println("Balance after deposite : "+balance);
53+
54+
}catch(InterruptedException o)
55+
{
56+
System.out.println(o);
57+
}
58+
}
59+
public void withdraw()
60+
{
61+
try {
62+
System.out.println("Balance before withdraw : "+balance);
63+
Thread.sleep(20000);
64+
System.out.println("Enter the withdraw ammount");
65+
withdraw=sc.nextInt();
66+
Thread.sleep(10000);
67+
68+
balance=balance-withdraw;
69+
Thread.sleep(1500);
70+
71+
System.out.println("Balance after withdraw : "+balance);
72+
73+
}catch(InterruptedException o)
74+
{
75+
System.out.println(o);
76+
}
77+
}
78+
}
79+
public class ThreadDemo16 {
80+
public static void main(String[]args)
81+
{
82+
Bank12 b=new Bank12();
83+
HusbendThread h=new HusbendThread(b);
84+
WifeThread w=new WifeThread(b);
85+
}
86+
87+
}
88+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package practice;
2+
3+
import java.util.Scanner;
4+
class HusbendThread11 implements Runnable{
5+
Thread husbend;
6+
Axis a;
7+
HusbendThread11(Axis a)
8+
{
9+
husbend=new Thread(this,"Hitesh");
10+
this.a=a;
11+
husbend.start();
12+
}
13+
public void run()
14+
{
15+
a.deposite();
16+
}
17+
}
18+
class WifeThread11 implements Runnable{
19+
Thread wife;
20+
Axis a;
21+
WifeThread11(Axis a)
22+
{
23+
wife=new Thread(this,"Hitesh");
24+
this.a=a;
25+
wife.start();
26+
}
27+
public void run()
28+
{
29+
a.withdraw();
30+
}
31+
32+
}
33+
class Axis{
34+
volatile int balance =50000;
35+
private int deposite;
36+
private int withdraw;
37+
Scanner sc=new Scanner(System.in);
38+
39+
public synchronized void deposite()
40+
{ try {
41+
System.out.println("Balance brfore deposite :"+balance);
42+
Thread.sleep(1000);
43+
System.out.println("Enter the deposite amount");
44+
deposite=sc.nextInt();
45+
Thread.sleep(500);
46+
balance=balance+deposite;
47+
Thread.sleep(500);
48+
System.out.println("Balance after deposite is :"+balance);
49+
}catch(InterruptedException k)
50+
{
51+
System.out.println(k);
52+
}
53+
}
54+
public synchronized void withdraw()
55+
{ try {
56+
System.out.println("Balance brfore withdraw :"+balance);
57+
Thread.sleep(1000);
58+
System.out.println("Enter the withdraw amount");
59+
withdraw=sc.nextInt();
60+
Thread.sleep(500);
61+
balance=balance-withdraw;
62+
Thread.sleep(500);
63+
System.out.println("Balance after withdraw is :"+balance);
64+
}catch(InterruptedException k)
65+
{
66+
System.out.println(k);
67+
}
68+
}
69+
}
70+
public class ThreadDemo17 {
71+
public static void main(String[]args)
72+
{
73+
Axis a=new Axis();
74+
HusbendThread11 h=new HusbendThread11(a);
75+
WifeThread11 w=new WifeThread11(a);
76+
}
77+
78+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package practice;
2+
3+
import java.util.Scanner;
4+
class HusbendThread159 implements Runnable{
5+
Thread husbend;
6+
SBI s;
7+
Scanner sc=new Scanner(System.in);
8+
HusbendThread159(SBI s)
9+
{
10+
this.s=s;
11+
husbend=new Thread(this,"Hitesh");
12+
husbend.start();
13+
}
14+
public void run()
15+
{ try {
16+
synchronized (s) {
17+
System.out.println("Balance is before deposite :"+s.balance);
18+
19+
System.out.println("Enter the deposite value");
20+
int hdeposite=sc.nextInt();
21+
22+
s.balance=s.deposite(hdeposite);
23+
24+
System.out.println("After depsosite amount is" +s.balance);
25+
}
26+
}catch(Exception i)
27+
{
28+
System.out.println(i);
29+
}
30+
31+
}
32+
}
33+
class WifeThread159 implements Runnable{
34+
Thread wife;
35+
SBI s;
36+
Scanner sc=new Scanner(System.in);
37+
WifeThread159(SBI s)
38+
{
39+
this.s=s;
40+
wife=new Thread(this,"Hitesh");
41+
wife.start();
42+
}
43+
public void run()
44+
{ try {
45+
synchronized (s) {
46+
System.out.println("Balance is before withdraw :"+s.balance);
47+
48+
System.out.println("Enter the withdraw value");
49+
int Wwithdraw=sc.nextInt();
50+
51+
s.balance=s.withdraw(Wwithdraw);
52+
53+
System.out.println("After withdraw amount is" +s.balance);
54+
}
55+
}catch(Exception i)
56+
{
57+
System.out.println(i);
58+
}
59+
60+
}
61+
62+
}
63+
64+
class SBI{
65+
volatile int balance=50000;
66+
private int deposite;
67+
private int withdraw;
68+
Scanner sc=new Scanner(System.in);
69+
70+
public int deposite(int hdeposite)
71+
{
72+
try {
73+
balance=balance+hdeposite;
74+
Thread.sleep(500);
75+
76+
}catch(InterruptedException k)
77+
{
78+
System.out.println(k);
79+
}
80+
return balance;
81+
}
82+
public int withdraw(int Wwithdraw)
83+
{
84+
try {
85+
balance=balance-Wwithdraw;
86+
Thread.sleep(500);
87+
88+
}catch(InterruptedException k)
89+
{
90+
System.out.println(k);
91+
}
92+
return balance;
93+
}
94+
}
95+
public class ThreadDemo18 {
96+
97+
}

0 commit comments

Comments
 (0)