Java multithreading Inter-thread process

What is Java multithreading Inter-thread process?

In Java multithreading process we face producer-consumer problem. It is called Inter-thread process. We can solve this problem using wait() and notify() methods in java programs.

 

In this java multithreading blog, 1st program we will see inter-thread problem. And 2nd program we will see solution of inter-thread process.

 

1st Program

Item Class in item package

package item;

public class Item

{

            public int x_item;

            synchronized public void put(int p)

            {

                        this.x_item = p;

                        System.out.println(“Put : ” + this.x_item);

            }

            synchronized public void get()

            {

                        System.out.println(“Get : ” + this.x_item);

            }

}

Producer class in classStore1 package

package classStore1;

import item.*;

public class Producer implements Runnable

{

            Item itr;

            Thread t;

            public Producer(Item itr)

            {

                        t = new Thread(this);

                        this.itr = itr;

                        t.start();

            }

            public void run()

            {

                        int i=1;

                        while(true)

                        {

                                    itr.put(i);

                                    i++;

                        }

            }

}

Consumer Class in classStore1 package

package classStore1;

import item.Item;

public class Consumer implements Runnable

{

            Item itr;

            Thread t;

            public Consumer(Item itr)

            {

 

                        t = new Thread(this);

                        this.itr = itr;

                        t.start();

            }

            public void run()

            {

                        while(true)

                        {

                                    itr.get();            // call to get() in item class

                        }

            }

}

 

ThreadEx9 class in examples Package

package examples;

import classStore1.*;

import item.*;

 

public class ThreadEx9

{

                public static void main(String[] args)

                {

                                Item obj = new Item();

                                Producer p1 = new Producer(obj);

                                Consumer c1 = new Consumer(obj);      

                }

}

 

Output

In this program, we will face inter-thread process. Check output in video


2nd Program

Item1 class in item package

package item;

public class Item1

{

                public int x_item;

                boolean b = false;

                synchronized public void put(int p)

                {

                                try

                                {

                                                if(b)

                                                {

                                                                wait();

                                                }

                                                else

                                                {

                                                                this.x_item = p;

                                                                System.out.println(“Put : ” + this.x_item);

                                                                notify();

                                                                b = true;

                                                }

                                }

                                catch(Exception ex){}

                }

                synchronized public void get()

                {

                                try

                                {

                                                if(b)

                                                {

                                                                System.out.println(“Get : ” + this.x_item);

                                                                notify();

                                                                b = false;

                                                }

                                                else

                                                {

                                                                wait();

                                                }

                                }

                                catch(Exception ex){}

                }

}

Producer1 Class in classStore1 package

Note: Producer1 class is same as Producer class. Just change 2 things.

 1.class name Producer to Producer1

2.constructor name Producer(Item itr) to Producer1(Item1 itr)

 

Consumer1 Class in classStore1 package

Note: Consumer 1 class is same as Consumer class. Just change 2 things.

 1.class name Consumer to Consumer 1

2.constructor name Consumer (Item itr) to Consumer 1(Item1 itr)

 

ThreadEx10 class in examples package

Note: ThreadEx10 class is same as ThreadEx9 class. Just change 4 things.

Class name ThreadEx9 to ThreadEx10

Item obj = new Item(); to Item1 obj = new Item1();

Producer p1 = new Producer(obj); to Producer1 p1 = new Producer1(obj);

Consumer c1 = new Consumer(obj); to Consumer1 c1 = new Consumer1(obj);

 

Output

In this program, we will solve inter-thread process. Check output in video

 


Video also available on YouTube Java Program Channel


If you have any query on blog example, you can directly comment us or subscribe our website


Earn Money Online

944total visits,3visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image