The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Separate Instance Threads
Discuss Separate Instance Threads in the Java Help forum on Dev Shed. Separate Instance Threads Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 30th, 2012, 02:13 AM
|
|
Registered User
|
|
Join Date: Aug 2008
Posts: 15
Time spent in forums: 9 h 40 m 12 sec
Reputation Power: 0
|
|
|
Thread troubles
Being new to threads, I'm finding it difficult to wrap my head around how to create separate instances of the same class being executed by multiple threads without those threads sharing any memory or data values except for ones I explicitly pass.
For Example:
java Code:
Original
- java Code |
|
|
|
decendants.add(tempLine); System. out. println(Thread. currentThread(). getName() + " " + tempLine + " decendant#:" + decendants. size() + " gen:" + gens ); SomeClass t = new SomeClass(); t.Code = tempLine; t.decendants = decendants; t.maxGenHolders = maxGenHolders; t.gens = gens + 1; sc.nextLine(); thread1.start();
All of the gens are sometimes output as different values. The two outputs in the center particularly frustrate me because they should be outputting the same value.
Why does t.gens = gens + 1; modify gens in the current instance?
Thankss!
|

December 30th, 2012, 06:04 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
|
Can you post the code for a small complete program that compiles, executes and shows the problem? The posted code can't be compiled and executed for testing.
Also can you post the output from the program and explain what is wrong with the output and show what you expect the output to be.
|

December 30th, 2012, 10:03 AM
|
|
Registered User
|
|
Join Date: Aug 2008
Posts: 15
Time spent in forums: 9 h 40 m 12 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by NormR Can you post the code for a small complete program that compiles, executes and shows the problem? The posted code can't be compiled and executed for testing.
Also can you post the output from the program and explain what is wrong with the output and show what you expect the output to be. |
java Code:
Original
- java Code |
|
|
|
import java.net.*; import java.io.*; import java.util.*; import javax.swing.*; import java.awt.image.*; import javax.imageio.*; import java.awt.event.*; import java.awt.Color; { int maxGen = 0, gens; public CheckDecendantNumber () throws Exception { frame = new JFrame("Decendant # checker"); java. awt. Container contentPane = frame. getContentPane(); panel1.add(textField); panel1.add(button); button.addActionListener(this); frame.add(panel1); frame.add(panel2); frame.setVisible(true); frame.pack(); } { CheckDecendantNumber app = new CheckDecendantNumber(); } { if(e.getSource() == button) { try { DragonThread t = new DragonThread(); t.Code = textField.getText(); t.decendants = decendants; t.maxGenHolders = maxGenHolders; thread1.start(); System. out. println("Decendant count:" + decendants. size()); System. out. println("Largest Generation in lineage: " + maxGen ); System. out. println("Largest Generation Holders:"); } { } } } } { static int maxGen, gens; public void run() { { //We've been interrupted: no more crunching. return; } getChildren(); } public void getChildren() { if(gens > maxGen-1) { maxGen = gens; maxGenHolders.clear(); } if(gens == maxGen) { maxGenHolders.add(Code); } try { URL url = new URL("http://dragcave.net/progeny/" + Code ); while ((inputLine = in.readLine()) != null) { Scanner sc = new Scanner (System. in); //System.out.println(inputLine); //sc.nextLine(); //System.out.println(inputLine); if(inputLine.contains("<div class=\'d\'")) { inputLine = inputLine.substring(inputLine.indexOf("<div class=\'d\'")); String tempLine = inputLine. substring(inputLine. indexOf("<a href"), inputLine. indexOf("</a>")); tempLine = tempLine.substring(tempLine.indexOf("view/")+5, tempLine.indexOf("\">")); if(!decendants.contains(tempLine) && !tempLine.equals(Code)) { decendants.add(tempLine); System. out. println(Thread. currentThread(). getName() + " " + tempLine + " decendant#:" + decendants. size() + " gen:" + gens ); DragonThread t = new DragonThread(); t.Code = tempLine; t.decendants = decendants; t.maxGenHolders = maxGenHolders; System. out. println(gens ); //this prints out something different t.gens = gens + 1; System. out. println(gens ); //than this sc.nextLine(); thread1.run();//start(); } } } } { getChildren(); //e.printStackTrace(); } } }
My test case is "1JLm"
Im expecting the output of gens to be the same number all 3 times. But it appears that gens in the current instance is being modified somehow.
|

December 30th, 2012, 10:16 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
Quote: | it appears that gens in the current instance is being modified somehow. |
Can you post the program's output and add some comments to it explaining what is unexpected about it?
Do you know what using the static attribute for a variable means?
When I enter: 1JLm in the text field, this is displayed:
Decendant count:0
Largest Generation in lineage: 0
Largest Generation Holders:
Thread-2 kkEE decendant#:1 gen:0
0
1
The program doesn't exit after displaying the above???
|

December 30th, 2012, 06:51 PM
|
|
Registered User
|
|
Join Date: Aug 2008
Posts: 15
Time spent in forums: 9 h 40 m 12 sec
Reputation Power: 0
|
|
Well I feel foolish now. While I was modifying the code I quickly added the gens declaration onto the end of another static variable. I guess all I needed was another set of eyes.... I suppose I should have just went to sleep instead of posting.
And no the code doesn't exit after that section of output. I added in a scanner.nextLine() in order to see portions of output at a time. With that test case I provided, it would probably take a few hours to execute anyway.
java Code:
Original
- java Code |
|
|
|
import java.net.*; import java.io.*; import java.util.*; import javax.swing.*; import java.awt.event.*; { int maxGen = 0, gens; public CheckDecendantNumber () throws Exception { frame = new JFrame("Decendant # checker"); java. awt. Container contentPane = frame. getContentPane(); panel1.add(textField); panel1.add(button); button.addActionListener(this); frame.add(panel1); frame.add(panel2); frame.setVisible(true); frame.pack(); } { CheckDecendantNumber app = new CheckDecendantNumber(); } { if(e.getSource() == button) { try { DragonThread t = new DragonThread(); t.Code = textField.getText(); t.decendants = decendants; t.maxGenHolders = maxGenHolders; thread1.start(); System. out. println("Decendant count:" + decendants. size()); System. out. println("Largest Generation in lineage: " + maxGen ); System. out. println("Largest Generation Holders:"); } { } } } } { static int maxGen; int gens; public void run() { { //We've been interrupted: no more crunching. return; } getChildren(); } public void getChildren() { if(gens > maxGen-1) { maxGen = gens; maxGenHolders.clear(); } if(gens == maxGen) { maxGenHolders.add(Code); } try { URL url = new URL("http://dragcave.net/progeny/" + Code ); while ((inputLine = in.readLine()) != null) { Scanner sc = new Scanner (System. in); //System.out.println(inputLine); //sc.nextLine(); //System.out.println(inputLine); if(inputLine.contains("<div class=\'d\'")) { inputLine = inputLine.substring(inputLine.indexOf("<div class=\'d\'")); String tempLine = inputLine. substring(inputLine. indexOf("<a href"), inputLine. indexOf("</a>")); tempLine = tempLine.substring(tempLine.indexOf("view/")+5, tempLine.indexOf("\">")); if(!decendants.contains(tempLine) && !tempLine.equals(Code)) { decendants.add(tempLine); System. out. println(Thread. currentThread(). getName() + " " + tempLine + " decendant#:" + decendants. size() + " gen:" + gens ); DragonThread t = new DragonThread(); t.Code = tempLine; t.decendants = decendants; t.maxGenHolders = maxGenHolders; t.gens = gens + 1; sc.nextLine(); thread1.run(); } } } } { getChildren(); //e.printStackTrace(); } } }
I'm still experiencing trouble with the gens variable being correctly computed during thread execution.
While the run method is being called without the use of threads as in the code above (line 121), it is 100% correct. However when changed to .start(), different values are displayed.
This is obviously a thread issue and something I'm overlooking like synchronized or something. But it is not obvious as to why different values are found. Each thread should be dealing only with its respective instance and the threads that it is creating. Nothing should be modifying the same values at the same time.
It should be acting like a tree. Say for example, you have a tree of nodes. I want to traverse the nodes from top to bottom using threads.
My goal is to send in a node, and create a thread for each child node.
Edit: Actually... I think I got it. The first generation can see the third generation and its possible for thread-x to get there before thread-y.
|

January 2nd, 2013, 11:22 PM
|
|
Registered User
|
|
Join Date: Aug 2008
Posts: 15
Time spent in forums: 9 h 40 m 12 sec
Reputation Power: 0
|
|
Say you had class with a static list of ints and an instance dependant variable.
A method in this class recursively creates threads of the same object in a loop.
Each thread adds an int to the static list and prints the size of the list.
The instance dependant variable is needed to keep track of where in the tree the current thread is.
java Code:
Original
- java Code |
|
|
|
import java.util.*; public class test { public static void main (String args []) { one t = new one(); thread.start(); } } { int instanceData; public void run() { count(); } public synchronized void count() { while(true) { one t = new one(); t.instanceData = instance data + 1; number.add(0); System. out. println(number. size() + " leaf # " + instance data ); thread.start(); } } }
Is there any way to synchronize separate instances?
Is there any way to keep track of data with threads without instances?
Or is there a better method that will make this work?
|

January 3rd, 2013, 09:31 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
|
The posted code has compiler errors. Can you post the current version of your working program so it can be compiled and executed for testing?
What is the posted code supposed to do?
The while(true) loop will execute forever until you use up all memory.
Last edited by NormR : January 3rd, 2013 at 09:36 AM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|