• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Acessing modified variables outside of thread

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to access a field 'path' from outside of a thread however from outside of the thread it appears to be empty but within the thread it is not empty? I've tried setting all fields modified to volatile but this hasn't worked. Has anyone got any ideas on how to get the correct value of the 'path' array? Thanks
The while loop is always true btw .

 
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I have been working on a project of mine, and it also needed to do the same thing you wanted to do.  
This approach is a bit, crude. But it works.  
Probably not the best, but I will post it anyways.  
Hope it helps  
I will reference "the class that holds the field you want to share" as Class1.
The other one, Class2.

1. Make the field you want to share static.
2. Add a Getter to Class1 that gets the field.
3. Add a parameter to Class2, that stores references to Class1.
4. Initiate Class2 inside the code in Class1, pass itself as the parameter(use 'this').
5. Begin running Class2 in another thread
6. Now it has access to Class1, which has a Getter for the field you want to share.

This worked for me when I was using JavaFX. It looks something like:

 
Ranch Hand
Posts: 134
5
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Coderanch!  

I would happy to see where do you define the variable

Please note that when you are writing quality code, you should always follow the coding standards. In Java you should always start a variable with a lowercase letter and follow the camelCase style when you are giving names to variables and methods. I hope you will provide the Path variable definition and where it is done.
 
Marshal
Posts: 65466
249
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afraid I have lots to say about your post. Sorry.

F Lucas wrote:. . .
1. Make the field you want to share static.

That sounds like a mistake to me. Only make something static if you have a good explanation for it. Otherwise I presume things are made static by mistake.

2. Add a Getter to Class1 that gets the field.

Is the field mutable, immutable, or a primitive? That makes a difference to how you write your getXXX() methods.

3. Add a parameter to Class2, that stores references to Class1.

I presume you mean field, because classes don't have parameters. Only constructors and methods have parameters. Don't let people tell you off for talking jargon, but make sure the jargon is used correctly. You probably meant to say, “a field that stores a reference to a Class2 object”. There are such things as references to objects, but not to classes.

4. Initiate Class2 inside the code in Class1, pass itself as the parameter(use 'this').

Say instantiate not initiate, and please explain more about what that sentence means; it isn't at all clear. When you send something to a parameter, call it an argument.

5. Begin running Class2 in another thread . . .

That is the same as you would do in a single‑threaded app. Don't say you are running a class; you don't run classes nor objects. You run their methods.

What bad names for fields, using single letters. Why are you using initiate() methods to set the fields rather than setting them via the constructor? If it is necessary those objects have a nonx2011;null reference to something else, there is no guarantee those initiate() methods will be called in time (or called at all), so you will have your objects in an inconsistent state. I am getting suspicious that you have created some sort of design with circular dependencies. That is a recipe for future trouble And why have you got thsoe casts in line 9‑13?

I don't know whether you have noticed, but line 16 shows that the standard FX technique for launching an application uses reflective instantiation of a class, maybe via Class#newInstance(), which many people regard as iffy programming. Newer versions of Java┬« deprecate that method.
 
F Lucas
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I kind of knew the jargon was going to be terribly mixed up.  
But about the code. I'm shocked about the amount of errors you found in that tiny snippet, and now I imagine how many is in my whole code...
I think I will check my code again.
Thanks for the reply.

 
Campbell Ritchie
Marshal
Posts: 65466
249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afraid I have a lot to say which I am not happy about. Sorry.

F Lucas wrote:. . . 1. Make the field you want to share static.

Why? I think you should only use the keyword static if you have a good explanation for it. Otherwise I presume any use of static is a serious error.

2. Add a Getter to Class1 that gets the field.

Is the field you are returning mutable, immutable, or a primitive? That makes a difference to how you write the getXXX() method.

3. Add a parameter to Class2, that stores references to Class1.

I found myself writing about jargon recently. Don't let people tell you off for speaking jargon, but be sure to use it correctly. Jargon is a language we use with other programmers, and it is very precise and concise, but can cause no end of confusion if used incorrectly.
You probably mean a field storing a reference to a Class1 object. Don't say anything about references to classes; there are only references to objects.
Only methods and constructors have parameters, which is what they receive. What is sent from elsewhere is called an argument. This bit of jargon varies from programming language to programming language.

4. Initiate Class2 inside the code in Class1, pass itself as the parameter(use 'this').

Please explain more. You probably mean instantiate rather than initiate, and your code doesn't show that sort of thing. I am getting worried seeing references passed to initiate() methods rather than constructors. That means there is no guarantee that your initiate() methods will be called in time. In fact, there is no way to be certain that those initiate() methods will be called at all. So you have your objects in an inconsistent state, with whichever field it is pointing to null for some of its lifetime. That is particularly hazardous if you are multi‑threading because its methods might be run in one thread before the object is in a consistent state.
I am also worried about why you are using those initiate() methods rather than constructors. It looks rather like code with a circular dependency, which is very iffy design, I am afraid.5. Begin running Class2 in another threadYou don't run classes, nor objects. You only run methods.

6. Now it has access to Class1, which has a Getter for the field you want to share.

This worked for me when I was using JavaFX. It looks something like:

You haven't shown how you are multi‑threading that app. What you have written doesn't seem to differ from a single‑threaded app.

Afraid I don't like the look of that code. Is it in a constructor, where it will be executed once, or in a method, where it might be executed repeatedly? What is going to happen to your static fields if you have multiple instances? Why have you used single letter variable names? Why have you got so many casts in line 9‑13? Lots of casts like that should make you think your inheritance is wrongly implemented.

I expect you didn't notice, but line 16 shows that FX uses reflective instantiation of the display class. I am not certain, but maybe that uses the Class#newInstance() method, which many people think is dubious practice. In fact, newer versions of Java┬« deprecate that method.

Sorry to appear so negative.
 
Campbell Ritchie
Marshal
Posts: 65466
249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, because I made a mistake pushing buttons, I appear to have written the same thing twice. Don't consider yourself as having been told off twice. Sorry.
 
F Lucas
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright.  
1. I made the field static, so it can be accessed by the other thread.  
Originally.  
Many sections of my code is becoming unnessesary after changes.  
Probably this is one of them too.  

2. The field I return is a reference to an object.
I was going to say it is immutable, but I did not explicitly make it so.  
It just does not needing to be mutable, because I create an instance, and then just get the data from an external resource.  
Its definitely not primitive, I can say that for sure.
Setter and Login needs Getter.
Browser needs Setter, Login, Getter, and print.

3.I meant to say 'Make the constructor of Class2 recieve an instance of Class1, and store the reference.'
By 'Initiate' I meant 'Set parameters AFTER instantiation, at a convinient timing'.
Which was nessesary, but probably will not be after I clean up the design.  
I thought the method name was accurate because when initiated, it would be considered dormant since it cannot do anything, and then you initialize it by giving the right classes.

4.initiate() is only called on the main method, once per object. The rest is handled by methods inside the FXBasedBrowser.
It does not have circular dependency, but its still quite messy indeed.  

6. I did not multithread it. When I launch the application, JavaFX creates another thread for it to run.
I do not know how to run it in one thread.

Basically, I created 5 interfaces.
Getter gets data from external sources
Setter stores data on external sources
Login is basically a getter, except having a reference to an instance of this object with a valid password gives you access to the methods. Used specifically for logging in.
Browser is the user interface.
The reason, is because I first had to make the core logic work by storing and reading from txt files, and creating a console based browser, where you type commands.
Then when I made MySQL versions of Getter,Setter, and Login, and made the FX version of Browser... Thats where things got messed up.
I'm just making the code work for now. And when it finally works, I will clean up the unnessesary parts.
But maybe that is a bad way to work with a project.
No need to say sorry. These kind of comments makes me realize things I would never do. And if I'm wrong, why should I not be criticized for it?
 
Campbell Ritchie
Marshal
Posts: 65466
249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

F Lucas wrote:. . . 1. I made the field static, so it can be accessed by the other thread.  . . .

There isn't any relationship between static status and accessibility. The other thread doesn't access the field, but other code running on that thread does. You would face the same problems of multiple access with instance fields.

2. The field I return is a reference to an object.
I was going to say it is immutable . . . I create an instance, and then just get the data from an external resource. . . .

It's mutable. It can change its state when accessed by code running on multiple threads, and it is therefore possible for the same field to be altered from two places simultaneously. It needs locking, otherwise it won't be thread‑safe. You are risking errors caused by half a field being updated from somewhere and the other half updated from elsewhere.

Setter and Login needs Getter.
Browser needs Setter, Login, Getter, and print.

But the code seems to show that a getter requires a setter. I can still perceive a circular dependency, where you can't instantiate A without B and you can't instantiate B without A.

. . . 'Set parameters AFTER instantiation, at a convinient timing'. . . .

That isn't clear, but you do have objects in an inconsistent state. There is no guarantee that line 11 will complete before line 13 starts, in which case the Getter reerence might be used by the browser before it is fully assigned, and it might still be null. There is also no guarantee that line 9 will have completed before line 13, in which case the Getter object might itself be in an inconsistent state.

4.initiate() is only called on the main method, once per object. . . .

How do you know? If those methods are public, then any code can call them again.

6. I did not multithread it. . . .

Yes, you did. You started executing code on the “main” thread, and then when the display appears, the code is run on whichever thread run s the display. There is an analogous thread in Swing, called the EDT; I can't remember what the FX thread is called.

Why aren't those initiate() methods declared in the interfaces? That would obviate the casts.

. . . No need to say sorry. . . .

It still isn't nice to be criticised, so I think, “sorry,” is appropriate.
 
F Lucas
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Learned a lot from that one. Going to fix those problems.  
Thanks for the reply.
 
Campbell Ritchie
Marshal
Posts: 65466
249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

F Lucas wrote:Learned a lot . . . Thanks . . .

That's a pleasure Show us the new improved product.
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!