Professional Documents
Culture Documents
Ex No: 6A Date: Multithreading in Java
Ex No: 6A Date: Multithreading in Java
MULTITHREADING IN JAVA
DATE:
AIM:
To write a Java program to create three threads. The first thread should print the values from
1 to 10. The second thread should print the values 11 to 20 and the third thread should print
values from 21 to 30.
ALGORITHM:
PROGRAM:
class Values
{
for(int i=n;i<n+10;i++)
{
try
{
Thread.sleep(100);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
Values n;
MyThread2(Values num)
{
n=num;
}
public void run()
{
n.printNum(11);
System.out.println(" -> thread 2 ");
}
}
class MyThread3 extends Thread
{
Values n;
MyThread3(Values num)
{
n=num;
}
public void run()
{
n.printNum(21);
System.out.println(" -> thread 3 ");
}
}
OUTPUT:
RESULT:
For the above program, the output was verified successfully.
EX NO: 6B
DEPOSIT AND WITHDRAWAL FUNCTIONS OF A BANK
DATE:
AIM:
To Write a Java program to write methods for deposit and withdrawal functions of a bank.
The methods should be synchronized in such a way when a user is depositing the amount, the
withdrawal should not occur.
ALGORITHM:
PROGRAM:
class Customer
{
int amount=12000;
System.out.println("going to withdraw...");
if(with_amt>amount)
{
try
{
wait(); // causes a thread to wait until it is notified.
}
catch(Exception e)
{
System.out.println(e);
}
}
amount=amount - with_amt;
System.out.println("Going to deposit...");
amount = amount + dep_amt;
}
}
class oops
{
};
};
t1.start();
t2.start();
}
}
OUTPUT:
RESULT:
For the above program, the output was verified successfully.