Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Constructing a threaded pipeline using buffer to sort a list of integers

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Constructing a threaded pipeline using buffer to sort a list of integers

    A thread pipeline is a sequence of threads linked together with a chain of buffers. Each thread in the pipeline reads from the buffer preceding it in the chain and may write to the buffer following it in the chain.

    Your task is to construct a pipeline that will sort a list of integer values. The pipe will consist of 10 threads only. Each thread will sort its own list of values.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Constructing a threaded pipeline using buffer to sort a list of integers

    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    harryjava (November 8th, 2013)

  4. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Constructing a threaded pipeline using buffer to sort a list of integers

    import java.util.*;
    import java.util.concurrent.locks.*;
    import java.io.*;
     
    public class Pipeline{
     
    	public static void main(String []args){
     
    		ArrayList<Integer>buffer =new ArrayList<Integer>(10);
     
    		new Thread(new Producer(buffer).      ).start();
    		new Thread(new Consumer(buffer)).start();
    	}
     
    	}
    class Producer implements Runnable{
     
    	ArrayList<Integer>buffer;
    	public Producer(ArrayList<Integer>k){
    		buffer=k;
    	}
    	Scanner in= new Scanner(new File("source.txt"));
    	public void run(){
    		while (in.hasNextLine()) {
                String s = in.nextLine();   
     
    			buffer.put(s);
    			in.close();
    			try{
    				Thread.sleep(100);
    			}catch(InterruptedException e){}
    		}
    	}
    }
     
    class Consumer implements Runnable{
    	Buffer <Integer>buffer;
    	public Consumer(Buffer<Integer>k){
    		buffer = k;
    	}
    	PrintWriter out = new PrintWriter(new FileWriter("dest.txt"));                    
    	public void run(){
    		System.out.println ("Buffer data");
    			Integer s =buffer.get();			
    			System.out.println (s+" ");
    			out.close();
    			try{
    				Thread.sleep(500);
    			}catch(InterruptedException e){}
     
     
    	}
    }
     
     
     
     
     
     
    class Buffer{
    	private int x;
    	private boolean item;
    	private Lock lock = new ReentrantLock();
    	private Condition full = lock.newCondition();
    	private Condition empty = lock.newCondition();
    	public Buffer(){item = false;}
    	public int read(){
    		lock.lock();
    		try{
    		  while(!item)
    			 try{full.await();}
    			 catch(InterruptedException e){}
    		  item = false;
    		  empty.signal();
    		  return x;
    	 }finally{lock.unlock();}
    	}
    	public void write(int k){
    		lock.lock();
    		try{
    	    while(item)
    		   try{empty.await();}
    		   catch(InterruptedException e){}
    	    x = k; item = true;
    	    full.signal();
    	  }finally{lock.unlock();}
      }
    }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Constructing a threaded pipeline using buffer to sort a list of integers

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Constructing a threaded pipeline using buffer to sort a list of integers

    Just did that there mate

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Constructing a threaded pipeline using buffer to sort a list of integers

    Ok, Now what are the problems you are having?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Constructing a threaded pipeline using buffer to sort a list of integers

    it seems to me that it is taking string and reading it into buffer then output to the consumer, i think i may have my wires crossed somewhere. I want the buffer to pass an arraylist of intergers to 10 threads each time the threads gets the list it sorts 10,000 and passes it on to the next thread to sort!!

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Constructing a threaded pipeline using buffer to sort a list of integers

    Do you have a new version of the code that compiles without errors? The posted code has several compiler errors.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Constructing a threaded pipeline using buffer to sort a list of integers

    No sorry this is all I have

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Constructing a threaded pipeline using buffer to sort a list of integers

    You need to fix the errors in the code so it can be compiled and executed.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Constructing a threaded pipeline using buffer to sort a list of integers

    thanks for your help anyways

Similar Threads

  1. how to sort an array of integers(eg using barcode)
    By jovita mateus in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 2nd, 2013, 02:09 AM
  2. How to sort integers on a file ascendingly?
    By my21 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 14th, 2013, 03:32 PM
  3. Sort and Merge Two Linked List
    By Loraeron in forum What's Wrong With My Code?
    Replies: 49
    Last Post: February 28th, 2013, 10:08 PM
  4. sort three integers!
    By Samule in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 17th, 2012, 02:18 PM
  5. [SOLVED] How to make a List containing integers.
    By mwebb in forum Java Theory & Questions
    Replies: 1
    Last Post: October 12th, 2011, 07:39 AM