Search This Blog

save java programs with".java" extension

examples :

helloworld.java
sample.java
match.java
send-mail.java

quick links

Showing posts with label multithread. Show all posts
Showing posts with label multithread. Show all posts

Thread Demo

// Create a second thread.
class NewThread implements Runnable {
Thread t;
NewThread() {
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ThreadDemo {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}

runnable example

class mythreadone implements Runnable
{
public void run()
{
for(int i=1;i<=10;i++)
System.out.println(i);
try
{
Thread.sleep(400);
}
catch(Exception e)
{
}
}
}

class mythreadtwo implements Runnable
{
public void run()
{
for(int j=11;j<=20;j++)
System.out.println(j);
try
{
Thread.sleep(400);
}
catch(Exception e)
{
}
}

}
class runnableexample {

public static void main(String[] args) {
Thread rt1=new Thread(new mythreadone());
Thread rt2=new Thread(new mythreadtwo());
rt1.start();
rt2.start();

}

}

priority example


class priorityexample {

public static void main(String[] args) {
Thread t1=Thread.currentThread();
System.out.println("this thread :"+t1.getName());
System.out.println("t1 priority :"+t1.getPriority());
Thread t2=new Thread();
System.out.println("t2 priority :"+t2.getPriority());
t1.setPriority(8);
Thread t3=new Thread();
System.out.println("t3 priority :"+t3.getPriority());

}

}

Multithreading banking application


class account
{
int accno;
float balance;
account(int accno,float balance)
{
this.accno=accno;
this.balance=balance;
}
void setbalance(float balance)
{
this.balance=balance;
}
float getbalance()
{
return this.balance;
}
void withdraw(float amount)
{
System.out.println("trying to withdraw (Rs) : "+amount+" from A/c : "+accno);
System.out.println("trying to get the balance info from A/c : "+accno);
//for(int i=1;i<20;i++)
// System.out.println(i);
float bal=getbalance();
System.out.println("balance in the A/c : "+accno+" is Rs "+bal);
if(bal>amount)
{
bal=bal-amount;
setbalance(bal);
System.out.println("please collect tha cash "+amount);
System.out.println("balance A/c "+accno+" update to Rs "+bal);
}
else
{
System.out.println("insufficient funds");
}

}
}
class accountthread extends Thread
{
account acc;
float amt;
accountthread(account acc,float amt)
{
this.acc=acc;
this.amt=amt;
}
public void run()
{
acc.withdraw(amt);
}
}

class multithreadbankingapplication {


public static void main(String[] args) throws Exception {
account a1=new account(1001,8000);
account a2=new account(1002,9000);
accountthread t1= new accountthread(a1,3000);
accountthread t2=new accountthread(a2,5000);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("final balance in the 1st account Rs"+a1.getbalance());
System.out.println("final balance in the 2nd account Rs"+a2.getbalance());

}

}

Multithread Demo

// Create multiple threads.
class NewThread1 implements Runnable {
String name; // name of thread
Thread t;
NewThread1(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + "Interrupted");
}
System.out.println(name + " exiting.");
}
}
class MultiThreadDemo {
public static void main(String args[]) {
new NewThread1("One"); // start threads
new NewThread1("Two");
new NewThread1("Three");
try {
// wait for other threads to end
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}