50) INTER THREAD COMMUNICATION

PHOTO EMBED

Mon Jul 08 2024 13:59:19 GMT+0000 (Coordinated Universal Time)

Saved by @varuntej #java

Producer-Consumer Inter-thread communication

Table.java 

public class Table
{
	private String obj;
	private boolean empty = true;
	 
	public synchronized void put(String obj)
	{  
	   if(!empty)
	    {   try
		    {  wait();
			}catch(InterruptedException e)
			{ e.printStackTrace(); }
		}
				
		this.obj = obj; 
		empty = false;
		System.out.println(Thread.currentThread().getName()+" has put "+obj);
		notify();
		
	}
	
	public synchronized void get()
	{   
	    if(empty)
	    {   try
		    {   wait();
			}catch(InterruptedException e)
			{ e.printStackTrace(); }
		}
		
		empty = true;
		System.out.println(Thread.currentThread().getName()+" has got "+obj);
		notify();		
	}
	
	public static void main(String[] args)
	{
		Table table = new Table();
		
		Runnable p = new Producer(table);
		Runnable c = new Consumer(table);
		
		Thread pthread = new Thread(p, "Producer");
		Thread cthread = new Thread(c, "Consumer");
		
		pthread.start(); cthread.start();
		
		try{ pthread.join(); cthread.join(); }
		catch(InterruptedException e){ e.printStackTrace(); }
	}
}

Producer.java

public class Producer implements Runnable
{   private Table table;

	public Producer(Table table)
	{ this.table = table; }
	
	public void run()
	{   java.util.Scanner scanner = new java.util.Scanner(System.in);
		for(int i=1; i <= 5; i++)
		{   System.out.println("Enter text: ");
	        table.put(scanner.next());	
            try{ Thread.sleep(1000); }	
			catch(InterruptedException e){ e.printStackTrace(); }		
		}
	}
}

Consumer.java

public class Consumer implements Runnable
{   
	private Table table;

	public Consumer(Table table)
	{ this.table = table; }
	
	public void run()
	{   for(int i=1; i <= 5; i++){ 
			table.get();
		}
		
	}
}

Inter-thread Synchronization example

Users.java


public class Users implements Runnable
{   private Account ac;
    
    public Users(Account ac)
    { this.ac = ac; }
	
    public void run()
    {   
        Thread t = Thread.currentThread();
         
        String name = t.getName();
		for(int i=1; i<=5; i++)
		{
			if (name.equals("John"))
			{  ac.deposit(200); } 
			if (name.equals("Mary"))
			{  ac.withdraw(200); }
		}
    }
}

Account.java

public class Account 
{   private double balance;
    
    public Account(double startBalance)
    { this.balance = startBalance; }
    	
    public   synchronized   void deposit(double amount)
    { Thread t = Thread.currentThread();
	  double bal = this.getBalance(); bal += amount;
	  try{ Thread.sleep(1000); }
      catch(InterruptedException ie)
      { ie.printStackTrace();  }
	  this.balance = bal;
	  System.out.println(t.getName() + " has deposited "+amount); }

    public  synchronized void withdraw(double amount)
    {   Thread t = Thread.currentThread();
        if (this.getBalance()>=amount)
        {   try{ Thread.sleep(1000); }
            catch(InterruptedException ie)
            { ie.printStackTrace();  }
            this.balance -= amount;
            System.out.println(t.getName() + " has withdrawn "+amount);
        }         
    }

    public double getBalance() {   return this.balance; }
}



AcTest.java

public class AcTest
{
    public static void main(String[] args)
    {
        Account a = new Account(5000);
        System.out.println("Current balance: "+a.getBalance());

        Runnable john = new Users(a); Runnable mary = new Users(a);

        Thread t1 = new Thread(john,"John");  Thread t2 = new Thread(mary,"Mary");
        t1.start(); t2.start();
        try{
            t1.join(); t2.join();
        }catch(InterruptedException ie)
        { ie.printStackTrace(); 
        }
        System.out.println("Current balance: "+a.getBalance());
    }
}

Synchronization another example     

Sync.java

public class Sync implements Runnable
{   private Display d;
    private String message;
	public Sync(Display d, String message)
	{ this.d = d; this.message = message; }
	
	public void run()
	{  synchronized(d)
         {      // or syncronize the show method
			d.show(this.message); 
	   }
	}
	
	public static void main(String[] args)
	{   Display d = new Display();
		Runnable r1 = new Sync(d, "First Message");  Runnable r2 = new Sync(d, "Second Message");
		Runnable r3 = new Sync(d, "Third message");
		Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); Thread t3 = new Thread(r3);
		t1.start(); t2.start();t3.start();
	}
}





class Display
{    // 	public synchronized void show(String message)
	public void show(String message)   
	{
		System.out.print("{ " + message);
		try{  Thread.sleep(500); }
		catch(InterruptedException e){ e.printStackTrace(); }
		System.out.println(" }");
	}
	
}
content_copyCOPY