Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8852447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:24:12+00:00 2026-06-14T13:24:12+00:00

I want to create application in which there are two thread One thread read

  • 0

I want to create application in which there are two thread
One thread read char data
another thread print it to console
I have following code to interThreadCommunication
(Assuming file has data like s t a c k o v e r f l o w)
I am generating ouput as following :

From echo int  115 char value s
From echo int  116 char value t
From echo int  97 char value a
From echo int  99 char value c
From echo int  107 char value k
From echo int  111 char value o
From echo int  118 char value v
From echo int  101 char value e
From echo int  114 char value r
From echo int  102 char value f
From echo int  108 char value l
From echo int  111 char value o
From echo int  119 char value w
From echo int  10 char value 

code :

import java.io.*;
class store
{
    int  i;
    public int get()
    {
        return i;
    }
    public void set(int i)
    {
        this.i=i;
    }
}
public class Main
{
    public static void main(String a[])
    {
        store s=new store();
        Thread th=new Thread(new read(s));
        Thread pr=new Thread(new echo(s));
        th.start();
        pr.start();             
    }   
}
class echo implements Runnable
{
    store st;
    public echo(store s)
    {
        st=s;
    }
    public void run()
    {       
        while(true)
        {
            int t=st.get();         
            if(t==-1)
                break;              
            if(t!=32)
            {
System.out.println("From echo int  "+t+" char value "+(char)st.get());  
            }
            st.set(0);
            try
            {
                //Thread.sleep(200);
                while(true)
                {
                    this.wait();
                    if(st.get()!=0)
                        break;

                }
            }
            catch(Exception r)
            {
            }
        }
    }   
}
class read implements Runnable
{
    FileReader fr=null;
    int r;
    store st;
    public read(store s)
    {
        st=s;
    }
    public void run()
    {
        try
        {
            fr=new FileReader("data.txt");
            while(true)
            {
                int r=fr.read();
                st.set(r);
                while(st.get()==0)
                {                   
                    this.wait();
                }   
                if(r==-1)
                    break;              
                try
                {
                    Thread.sleep(200);
                }
                catch(Exception re)
                {
                }
            }   
            st.set(-1);         
        }   
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {   
                fr.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();    
            }
        }               
    }
}

If I remove sleep() method from both of class read & echo then I am getting

From echo int  0 char value 
From echo int  0 char value 
From echo int  0 char value 
.
.
.

I am using wait() method to another thread can process.
I am doing in right manner or there is any another method for interThreadCommunication

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:24:14+00:00Added an answer on June 14, 2026 at 1:24 pm

    You should consider using the producer-consumer pattern:
    Thread A will read the entries from the file, and will put them in a queue.
    Thread B will read entries from the queue and will print them.
    You can use a blocking queue implementation for thread safety of the queue.
    I would also consider not to put one character to the queue, but maybe a group of characters (a word, or a line) ,
    In order to reduce number of enqueue and dequeue (as these methods are thread-safe, they have a higher performance penalty than non-synchronized methods).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

see basically i have one multi-thread application in which i want to see the
I want to create application which would display spatial data on Map. I'm thinking
I have a Homework Planner application which I want to create an Alarm for
i have created an application which show data from user current location. I want
In my application the main thread creates two joined threads; one which waits for
I want to create an application which main window has canvas (or something where
I want to create an iOS application which consists of Tamil language. I see
I want to create a simple server application which runs on the desktop, and
In my application I want to create a dialog which has a button to
I want to create a simple j2me application for a start which links with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.