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 8778145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:27:21+00:00 2026-06-13T19:27:21+00:00

The Question is as the following: Create 3 threads. first one will generate 10

  • 0

The Question is as the following:

Create 3 threads.

  • first one will generate 10 random numbers,
  • the second will sum the even of these 10.
  • the third will sum the odd of the same 10 random numbers.

My Problem is: In the 2nd thread i read all the numbers and push them back to the stream but when the 3rd thread wants to read from the stream, the first value read is -1 ?!

Here is the code:

//main program 
import java.io.*;
public class anonymous {

public static void main(String[] args) throws  IOException, InterruptedException {
    final PipedOutputStream out= new PipedOutputStream();  
    final PipedInputStream in= new PipedInputStream(out);
    Thread1 th1 = new Thread1(out);
    Thread2 th2 = new Thread2(in);
    Thread3 th3 = new Thread3(in); 
    Thread t1 = new Thread(th1);
    Thread t2 = new Thread(th2); 
    Thread t3 = new Thread(th3); 
    t1.start(); 
    t2.start();
    t2.join();
    t3.start(); 
    t3.join();
    System.out.println("main finished.");   
}
}

//Thread1
import java.io.*;
import java.util.Random;
public class Thread1 implements Runnable{
PipedOutputStream out=null;
Random r = new Random();
public Thread1(PipedOutputStream send){
    this.out = send; 
}
public void run(){
    int num;
    System.out.println("thread 1 generated random numbers: ");
    try{
    for ( int i=0; i<10; i++)
        {
        num=r.nextInt(10);
        System.out.print(num + " ");
        out.write(num);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } 
try {
    out.close();
} catch (IOException e) {
    e.printStackTrace();
}
System.out.println("\nthread 1 finished");
}
}

//Thread2
import java.io.*;
public class Thread2 implements Runnable{
PipedInputStream in=null;
public Thread2( PipedInputStream get){
    this.in = get;

}
public void run(){ 
    PushbackInputStream push = new PushbackInputStream(in , 10);
    //PushbackInputStream takes an InputStream and it will read the first 10 bytes
            // in the stream and push them back to the stream
    try {
        byte[] byteArr = new byte[10]; 
        int i, sum=0, idx=0;
        i=push.read();
        while (i != -1)
        { 
            if( i%2 == 0)
                sum += i; 
            byteArr[idx]=( byte) i;
            i=push.read();
            idx++;
        }
        push.unread(byteArr,0 , 10);
        System.out.println("thread 2: the sum of even random numbers: " + sum); 
    } catch (IOException e) {
        e.printStackTrace();
    }
System.out.println("thread 2 finished"); 
}
}

//Thread3
import java.io.*;
public class Thread3 implements Runnable{

PipedInputStream in;
public Thread3( PipedInputStream get){
    this.in = get;
}
public void run(){
    try {
        int i, sum=0;
        i=in.read();
        while (i != -1)
        { 
            if( i%2 == 1)
                sum += i; 
            i=in.read();
        }
        System.out.println("thread 3: the sum of odd random numbers:  " + sum);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        in.close();
    } catch (IOException e){
        e.printStackTrace();
    } 
System.out.println("thread 3 finished"); 
}
}

the output as follows:

thread 1 generated random numbers:
4 8 7 5 6 8 7 1 0 5
thread 1 finished
thread 2: the sum of even random numbers: 26
thread 2 finished
thread 3: the sum of odd random numbers: 0
thread 3 finished
main finished.

  • 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-13T19:27:22+00:00Added an answer on June 13, 2026 at 7:27 pm

    You should use another pipe between the second and third streams, instead of abusing the pushback feature.

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

Sidebar

Related Questions

What is the difference between the following maps I create (in another question, people
Following the CSS style trick from this question I was able to create a
Following my previous question , I need to create a value consisting of a
The following question was asked in one of my interview (few years back) What
Say, I have the following C++ code. First I create a manual waitable timer
I have the following situation: I create a boost::thread_group instance, then create threads for
I created the following simple PL/SQL stored procedure example to ask a specific question.
The question is: Is it possible to create a structure like the folowing using
I ask this question following the issue I raised here . The point is
The following question is not a typical programming question, but IMHO programmers are affected

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.