Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 30th, 2012, 02:13 AM
brynpttrsn brynpttrsn is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 15 brynpttrsn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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
  1. decendants.add(tempLine);
  2. System.out.println(Thread.currentThread().getName() + " " + tempLine + " decendant#:" + decendants.size() + " gen:" + gens);
  3. SomeClass t = new SomeClass();
  4. t.Code = tempLine;
  5. t.decendants = decendants;
  6. t.maxGenHolders = maxGenHolders;
  7. System.out.println(gens);
  8. t.gens = gens + 1;
  9. System.out.println(gens);
  10. Thread thread1 = new Thread(t);
  11. sc.nextLine();
  12. 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!

Reply With Quote
  #2  
Old December 30th, 2012, 06:04 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,950 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 27 m 39 sec
Reputation Power: 345
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.

Reply With Quote
  #3  
Old December 30th, 2012, 10:03 AM
brynpttrsn brynpttrsn is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 15 brynpttrsn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4. import javax.swing.*;
  5. import java.awt.image.*;
  6. import javax.imageio.*;
  7. import java.awt.event.*;
  8. import java.awt.Color;
  9.  
  10.  
  11. public class CheckDecendantNumber implements ActionListener
  12. {
  13.     List<JLabel> labelList = new LinkedList();
  14.     JPanel panel1 = new JPanel();
  15.     JPanel panel2 = new JPanel();
  16.     JPanel panel3 = new JPanel();
  17.     JFrame frame;
  18.     JButton button = new JButton("Check Decendants");
  19.     JTextField textField = new JTextField(20);
  20.     File file;
  21.     BufferedImage img;
  22.     List<String> decendants = new LinkedList<String>();
  23.     int maxGen = 0, gens;
  24.     List<String> maxGenHolders = new LinkedList<String>();
  25.  
  26.     public CheckDecendantNumber() throws Exception
  27.     {
  28.         frame = new JFrame("Decendant # checker");
  29.         java.awt.Container contentPane = frame.getContentPane();
  30.         panel1.add(textField);
  31.         panel1.add(button);
  32.         button.addActionListener(this);
  33.         contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
  34.         frame.add(panel1);
  35.         frame.add(panel2);
  36.         frame.setVisible(true);
  37.         frame.pack();
  38.     }
  39.     public static void main(String[] args) throws Exception
  40.     {
  41.         CheckDecendantNumber app = new CheckDecendantNumber();
  42.     }
  43.     public void actionPerformed(ActionEvent e)
  44.     {
  45.         if(e.getSource() == button)
  46.         {
  47.             try
  48.             {
  49.                 DragonThread t = new DragonThread();
  50.                 Thread thread1 = new Thread(t);
  51.                 t.Code = textField.getText();
  52.                 t.decendants = decendants;
  53.                 t.maxGenHolders = maxGenHolders;
  54.                 thread1.start();
  55.  
  56.                 System.out.println("Decendant count:" + decendants.size());
  57.                 System.out.println("Largest Generation in lineage: " + maxGen);
  58.                 System.out.println("Largest Generation Holders:");
  59.             }
  60.             catch(Exception err)
  61.             {
  62.                 System.out.println(err);
  63.             }
  64.         }
  65.     }
  66. }
  67. class DragonThread implements Runnable
  68. {
  69.     String Code;
  70.     static int maxGen, gens;
  71.     static List<String> decendants;
  72.     static List<String> maxGenHolders;
  73.     public void run()
  74.     {
  75.         if (Thread.interrupted())
  76.         {
  77.             //We've been interrupted: no more crunching.
  78.             return;
  79.         }
  80.         getChildren();
  81.     }
  82.     public void getChildren()
  83.     {
  84.         if(gens > maxGen-1)
  85.         {
  86.             maxGen = gens;
  87.             maxGenHolders.clear();
  88.         }
  89.         if(gens == maxGen)
  90.         {
  91.             maxGenHolders.add(Code);
  92.         }
  93.         try
  94.         {
  95.             String inputLine;
  96.             URL url = new URL("http://dragcave.net/progeny/" + Code);
  97.             BufferedReader in = new BufferedReader( new InputStreamReader( url.openStream()));
  98.  
  99.             while ((inputLine = in.readLine()) != null)
  100.             {
  101.                 Scanner sc = new Scanner(System.in);
  102.                 //System.out.println(inputLine);
  103.                 //sc.nextLine();
  104.                 //System.out.println(inputLine);
  105.                 if(inputLine.contains("<div class=\'d\'"))
  106.                 {
  107.                     inputLine = inputLine.substring(inputLine.indexOf("<div class=\'d\'"));
  108.                     String tempLine = inputLine.substring(inputLine.indexOf("<a href"), inputLine.indexOf("</a>"));
  109.                     tempLine = tempLine.substring(tempLine.indexOf("view/")+5, tempLine.indexOf("\">"));
  110.                     if(!decendants.contains(tempLine) && !tempLine.equals(Code))
  111.                     {
  112.                         decendants.add(tempLine);
  113.                         System.out.println(Thread.currentThread().getName() + " " + tempLine + " decendant#:" + decendants.size() + " gen:" + gens);
  114.  
  115.                         DragonThread t = new DragonThread();
  116.                         Thread thread1 = new Thread(t);
  117.  
  118.                         t.Code = tempLine;
  119.                         t.decendants = decendants;
  120.                         t.maxGenHolders = maxGenHolders;
  121.  
  122.                         System.out.println(gens);//this prints out something different
  123.                         t.gens = gens + 1;
  124.                         System.out.println(gens);//than this
  125.  
  126.                         sc.nextLine();
  127.                         thread1.run();//start();
  128.                     }
  129.                 }
  130.             }
  131.         }
  132.         catch(Exception e)
  133.         {
  134.             System.out.println("Retry!");
  135.             getChildren();
  136.             //e.printStackTrace();
  137.         }
  138.     }
  139. }

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.

Reply With Quote
  #4  
Old December 30th, 2012, 10:16 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,950 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 27 m 39 sec
Reputation Power: 345
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???

Reply With Quote
  #5  
Old December 30th, 2012, 06:51 PM
brynpttrsn brynpttrsn is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 15 brynpttrsn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4. import javax.swing.*;
  5. import java.awt.event.*;
  6.  
  7.  
  8. public class CheckDecendantNumber implements ActionListener
  9. {
  10.     List<JLabel> labelList = new LinkedList();
  11.     JPanel panel1 = new JPanel();
  12.     JPanel panel2 = new JPanel();
  13.     JPanel panel3 = new JPanel();
  14.     JFrame frame;
  15.     JButton button = new JButton("Check Decendants");
  16.     JTextField textField = new JTextField(20);
  17.     List<String> decendants = new LinkedList<String>();
  18.     int maxGen = 0, gens;
  19.     List<String> maxGenHolders = new LinkedList<String>();
  20.  
  21.     public CheckDecendantNumber() throws Exception
  22.     {
  23.         frame = new JFrame("Decendant # checker");
  24.         java.awt.Container contentPane = frame.getContentPane();
  25.         panel1.add(textField);
  26.         panel1.add(button);
  27.         button.addActionListener(this);
  28.         contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
  29.         frame.add(panel1);
  30.         frame.add(panel2);
  31.         frame.setVisible(true);
  32.         frame.pack();
  33.     }
  34.     public static void main(String[] args) throws Exception
  35.     {
  36.         CheckDecendantNumber app = new CheckDecendantNumber();
  37.     }
  38.     public void actionPerformed(ActionEvent e)
  39.     {
  40.         if(e.getSource() == button)
  41.         {
  42.             try
  43.             {
  44.                 DragonThread t = new DragonThread();
  45.                 Thread thread1 = new Thread(t);
  46.                 t.Code = textField.getText();
  47.                 t.decendants = decendants;
  48.                 t.maxGenHolders = maxGenHolders;
  49.                 thread1.start();
  50.  
  51.                 System.out.println("Decendant count:" + decendants.size());
  52.                 System.out.println("Largest Generation in lineage: " + maxGen);
  53.                 System.out.println("Largest Generation Holders:");
  54.             }
  55.             catch(Exception err)
  56.             {
  57.                 System.out.println(err);
  58.             }
  59.         }
  60.     }
  61. }
  62. class DragonThread implements Runnable
  63. {
  64.     String Code;
  65.     static int maxGen;
  66.     int gens;
  67.     static List<String> decendants;
  68.     static List<String> maxGenHolders;
  69.     public void run()
  70.     {
  71.         if (Thread.interrupted())
  72.         {
  73.             //We've been interrupted: no more crunching.
  74.             return;
  75.         }
  76.         getChildren();
  77.     }
  78.     public void getChildren()
  79.     {
  80.         if(gens > maxGen-1)
  81.         {
  82.             maxGen = gens;
  83.             maxGenHolders.clear();
  84.         }
  85.         if(gens == maxGen)
  86.         {
  87.             maxGenHolders.add(Code);
  88.         }
  89.         try
  90.         {
  91.             String inputLine;
  92.             URL url = new URL("http://dragcave.net/progeny/" + Code);
  93.             BufferedReader in = new BufferedReader( new InputStreamReader( url.openStream()));
  94.  
  95.             while ((inputLine = in.readLine()) != null)
  96.             {
  97.                 Scanner sc = new Scanner(System.in);
  98.                 //System.out.println(inputLine);
  99.                 //sc.nextLine();
  100.                 //System.out.println(inputLine);
  101.                 if(inputLine.contains("<div class=\'d\'"))
  102.                 {
  103.                     inputLine = inputLine.substring(inputLine.indexOf("<div class=\'d\'"));
  104.                     String tempLine = inputLine.substring(inputLine.indexOf("<a href"), inputLine.indexOf("</a>"));
  105.                     tempLine = tempLine.substring(tempLine.indexOf("view/")+5, tempLine.indexOf("\">"));
  106.                     if(!decendants.contains(tempLine) && !tempLine.equals(Code))
  107.                     {
  108.                         decendants.add(tempLine);
  109.                         System.out.println(Thread.currentThread().getName() + " " + tempLine + " decendant#:" + decendants.size() + " gen:" + gens);
  110.  
  111.                         DragonThread t = new DragonThread();
  112.                         Thread thread1 = new Thread(t);
  113.  
  114.                         t.Code = tempLine;
  115.                         t.decendants = decendants;
  116.                         t.maxGenHolders = maxGenHolders;
  117.  
  118.                         t.gens = gens + 1;
  119.  
  120.                         sc.nextLine();
  121.                         thread1.run();
  122.                     }
  123.                 }
  124.             }
  125.         }
  126.         catch(Exception e)
  127.         {
  128.             System.out.println("Retry!");
  129.             getChildren();
  130.             //e.printStackTrace();
  131.         }
  132.     }
  133. }


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.

Reply With Quote
  #6  
Old January 2nd, 2013, 11:22 PM
brynpttrsn brynpttrsn is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 15 brynpttrsn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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
  1. import java.util.*;
  2.  
  3. public class test
  4. {
  5.     public static void main(String args[])
  6.     {
  7.         one t = new one();
  8.         Thread thread = new Thread(t);
  9.         thread.start();
  10.     }
  11. }
  12. class one implements Runnable
  13. {
  14.     static List<Integer> number = new LinkedList();
  15.     int instanceData;
  16.  
  17.     public void run()
  18.     {
  19.         count();
  20.     }
  21.     public synchronized void count()
  22.     {
  23.         while(true)
  24.         {
  25.             one t = new one();
  26.             Thread thread = new Thread(t);
  27.             t.instanceData = instance data + 1;
  28.             number.add(0);
  29.             System.out.println(number.size() + " leaf # " + instance data);
  30.             thread.start();
  31.         }
  32.     }
  33. }


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?

Reply With Quote
  #7  
Old January 3rd, 2013, 09:31 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,950 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 27 m 39 sec
Reputation Power: 345
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Separate Instance Threads

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap