Results 1 to 20 of 20
Thread: Multithreading (Three Threads)
- 05-03-2011, 05:25 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
Multithreading (Three Threads)
Hi everybody :)
I just need your help to teach me how can I programing this example.
Just explain and guide me to how to program , DON'T give me any solution please :p
OK, the idea of the program is a three threads they are working together synchronizing.
the first thread reading data - positive integer numbers - only from the keyboard, entered by a user to be stored in an array -dynamic array -
Assume that the array can hold all numbers entered without overflow.
second thread should read data from the array and write them into a file.
third thread should start reading the array data as well as the file data and display them on the screen in order.
please I want to understanding how programing this example.
waiting you.
- 05-03-2011, 08:07 PM #2
Were you instructed to use three threads? If not, I suggest simplifying. Normally this would call for only one thread. If the objective is to learn about threads and synchronization, perhaps you should start with just two: the main thread, and one other you explicitly create.
One thread could accept input from the keyboard and store it in the array, and the other could read data from the array and write it to a file. The threads would synchronize on the array. This would be a good opportunity to learn about Object.wait() and Object.notify().
- 05-04-2011, 12:14 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
thank you Kjkrum, Yes I should use 3 threads.
I am trying to use wait() and notify() but I don't know how the correct way to use them !
I try to do first and second thread
second threadJava Code:public class Thread1 extends Thread { ArrayList s = new ArrayList<Integer>(); Scanner sc = new Scanner(System.in); int num = 0; public void run(){ System.out.println("Enter a positive number, or negitavie to exit :"); while(num >=0){ num = sc.nextInt(); s.add(num); } } }
this of course will not work correctly,Java Code:public class Thread2 extends Thread { FileOutputStream fp; Producer obj1 = null ; public void run(){ try{ fp = new FileOutputStream("Q_2.txt"); new PrintStream(fp).println(obj1.s); fp.close(); } catch(IOException e){ } } }
so I need your help, how can I program the third thread which read from the two previous thread and how can I let this 3 thread work together synchronizing ?
- 05-04-2011, 12:25 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
As a tangent, I'd like to suggest that you implement runnable rather than extend thread. Extension intends you are going to change the abilities of the class somehow, and in these classes you are not. Also, since you can only extend one class it may be more helpful to implement runnable, leaving the ability to extend another class if needed.
Is the change and really doesn't require any changes(it still requires the implementation of run.)Java Code:class MyThreadExample implements Runnable
Implementing vs. Extending(Stackoverflow)
- 05-04-2011, 12:36 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
thank you sunde887, yes I understand the different between "extends" and "implements",
thank you,
any help please ?
- 05-04-2011, 12:39 AM #6
First, give both your threads a reference to the same ArrayList object. Perhaps declare it in your main method, and pass it to your Thread1 and Thread2 classes in their constructors.
Using wait() and notify() is pretty simple once you understand it. To call either method on an object, a thread must first obtain the lock on that object with synchronized:
When a thread calls wait() on an object, it automatically releases all its locks and goes to sleep. When another thread calls notify() on that same object, the sleeping thread starts trying to reacquire its locks. Once it acquires all the locks it had before going to sleep, it resumes execution immediately after the call to wait().Java Code:ArrayList<Integer> list = new ArrayList<Integer>(); ... synchronized(list) { // make changes to the list, and then call // wait or notify within this synchronized block }
So... your thread that's reading from the ArrayList and writing to the file could synchronize on the list, read everything from it, and then wait() on it, in a loop. The thread that's writing to the list would synchronize on the list, write to it, and call notify() on it, also in a loop.
Hope that gets you going in the right direction.
- 05-04-2011, 12:59 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
how can I do that , can you help me please :o ,First, give both your threads a reference to the same ArrayList object. Perhaps declare it in your main method, and pass it to your Thread1 and Thread2 classes in their constructors.
- 05-04-2011, 01:08 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
If they both have an instance variable which is an array list then in main you can pass in the correct array list through a constructor.
in this example, the thread named y has a reference to the array list.Java Code:class X extends Thread{ ArrayList<SomeType> x; public X(ArrayList<SomeType> x){ this.x = x; } } public class SomeType{ public static void main(String[] args){ ArrayList<SomeType> x = new ArrayList<SomeType>(); X y = new X(x); } }
- 05-04-2011, 01:24 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
thank you so much sunde887, really helpful :)
--------
OK, I am trying to write the first two thread like this
first one:
second one:Java Code:public class Thread1 extends Thread { ArrayList<Integer> ar1 = new ArrayList<Integer>(); int num = 0; public Thread1(ArrayList<Integer> x){ this.ar1 = x; } public int scan(){ Scanner sc = new Scanner(System.in); return sc.nextInt(); } public void run(){ System.out.println("Enter a positive number, or negitavie to exit :"); try{ num = scan(); while(num >=0){ ar1.add(num); num = scan(); } } catch(Exception e){ } } }
& main:Java Code:public class Thread2 extends Thread { PrintWriter fp; ArrayList<Integer> ar1 = new ArrayList<Integer>(); public Thread2(ArrayList<Integer> x){ this.ar1 = x; } public void read(){ try{ fp = new PrintWriter("Q_2.txt"); } catch(IOException e){ } } public void run(){ read(); fp.println(ar1); close(); } public void close(){ fp.close(); } }
any one teach me to next steps please :/Java Code:public class Main { public static void main(String[] args) { ArrayList<Integer> array = new ArrayList<Integer>(); Thread1 t1 = new Thread1(array); Thread2 t2 = new Thread2(array); t1.start(); t2.start(); } }
~Last edited by Faith; 05-04-2011 at 01:57 AM.
- 05-05-2011, 01:29 AM #10
- 05-05-2011, 03:02 AM #11
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
I am sorry but can you guide me how to do that :( ,,
- 05-05-2011, 03:14 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I need to preface this with two things, first, a warning: my threading skills are lackluster at best so my help may not be exceptionally great.
The others thing: you will get more help if you keep trying things and posting your attempts. Read the concurrency section if it helps.
That being said, here is my advice.
The thread which reads the input may also maintain it's own personal array to store the data in. If the main array list is not locked it should acquire the lock and add it's personal contents to the main array.
The thread for writing the array list to the file will want to acquire the lock and then write the array list elements to the file.
The third thread needs to, when the lock is free, acquire the lock and then print all the contents to the console. You will need to synchronize on the main array. What have you been thinking of trying? What are your doubts? What do you need more clarification on?
The more specific your question is, the more likely you are to receive more help.
I wish I could help you more, however; like I said I haven't really gotten into threads, I'll be buying concurrency in practice soon so in two-three months I'll (hopefully) know more.
- 05-05-2011, 12:46 PM #13
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
sunde887 & kjkrum - thank you for your help very much.
if any one have a time and can be with me step by step I will be very thanks to him. I just want understanding deep details to be Professional in java in future.
only I want understand the concurrent and synchronizing.
How can I implement that ?
you said before that using of wait() and notify, and synchronizing the list of array right ? OK, but how ? I put my codes before, is it true where I put array list in main ?
is it true to create two array list object in all thread class ?
I apologize that if no one could afford my questions, and I will thankful to you even if you can not help me, just because I am new and beginner in terms of synchronization and I want someone explain to me what I do step by step.
if any one can afford my questions let's start please from first code I put.
is it true ?
should I add any methods ? wait(), notify, or synchronized ?
- 05-05-2011, 11:44 PM #14
Okay, I'll get you started by making some changes to your own Thread1 class.
I haven't tried to compile/run this, so I apologize for any typos.Java Code:public class Thread1 extends Thread { /* don't need to initialize this here, since it's initialized in the constructor */ ArrayList<Integer> ar1; /* moved this here from your scan() method... no need to create a new Scanner for every input */ Scanner sc = new Scanner(System.in); public Thread1(ArrayList<Integer> x){ this.ar1 = x; } public int scan(){ return sc.nextInt(); } public void run(){ System.out.println("Enter a positive number, or negitavie to exit :"); try{ while(!Thread.currentThread().isInterrupted()){ int num = scan(); if(num < 0) break; synchronized(ar1) { /* this has to be inside the synchronized block, or other threads might not be able to see the change */ ar1.add(num); /* tells your other thread, which has called ar1.wait(), to wake up and start reading from ar1 */ ar1.notify(); } } } catch(Exception e){ /* it's good to always at least print something when you catch an exception. */ e.printStackTrace(); } } }Last edited by kjkrum; 05-05-2011 at 11:49 PM.
- 05-06-2011, 07:36 AM #15
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
kjkrum thank you very much for your start and now I am very exciting ,,
I have only one question because other things very understood and code work correctly, thanks kjkrum.
in this part of code:
why you break the loop ? we need to write all number the user is input until he enter negative number the loop break and the second thread is start and run.Java Code:while(!Thread.currentThread().isInterrupted()){ int num = scan(); if(num < 0) break; synchronized(ar1) { /* this has to be inside the synchronized block, or other threads might not be able to see the change */ ar1.add(num); /* tells your other thread, which has called ar1.wait(), to wake up and start reading from ar1 */ ar1.notify(); } }
for second thread
at run() method I am trying to add wait() as you see,Java Code:public class Thread2 extends Thread { PrintWriter fp; ArrayList<Integer> ar1 = new ArrayList<Integer>(); public Thread2(ArrayList<Integer> x){ this.ar1 = x; } public void read(){ try{ fp = new PrintWriter("Q_2.txt"); } catch(IOException e){ } } public void run(){ try{ ar1.wait(); read(); fp.println(ar1); close(); } catch(Exception e){ } } public void close(){ fp.close(); } }
but the code does not work like I want at all :\
it does not write the numbers that the user entered.Last edited by Faith; 05-06-2011 at 11:22 PM.
- 05-06-2011, 11:37 PM #16
The break is part of the if statement above it.
You need to synchronize on ar1 before you can call wait() on it. Look at the synchronized block in the code I posted.
- 05-06-2011, 11:54 PM #17
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
really great,
thank you Kjkrum now the idea very clear,
but when I run the threads, thread which is write the input numbers is write only first number !! why it does not write all list ?
- 05-08-2011, 08:52 PM #18
Could you post the code?
- 05-08-2011, 08:54 PM #19
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
this the code :
Java Code:public class Thread2 extends Thread { PrintWriter fp; ArrayList<Integer> ar1 = new ArrayList<Integer>(); public Thread2(ArrayList<Integer> x){ this.ar1 = x; } public void read(){ try{ fp = new PrintWriter("Q_2.txt"); } catch(IOException e){ } } public void run(){ try{ synchronized(ar1){ ar1.wait(); read(); fp.println(ar1); close(); } } catch(Exception e){ } } public void close(){ fp.close(); } }
- 05-08-2011, 09:00 PM #20
Similar Threads
-
multithreading
By praveenbhushan1989 in forum Threads and SynchronizationReplies: 8Last Post: 04-17-2011, 03:03 PM -
Multithreading Gui
By BUGSIE91 in forum Threads and SynchronizationReplies: 7Last Post: 10-13-2010, 02:20 PM -
Want to know about Multithreading.
By Chetans in forum Threads and SynchronizationReplies: 1Last Post: 03-19-2010, 07:50 AM -
multithreading
By shilpa.krishna in forum New To JavaReplies: 2Last Post: 06-27-2008, 04:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks