File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Creating two Threads using same resource will affect the control flow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Creating two Threads using same resource will affect the control flow" Watch "Creating two Threads using same resource will affect the control flow" New topic
Author

Creating two Threads using same resource will affect the control flow

Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 220
Hi All,

Suppose



In Threadone.java


In the above code _str changes first time with integ, then creates the thread and calls method in another class(anotherClass in code) by passing _str as param. In this time the main thread executes and set the _str to string not integ. So when I pass _str first time its value becomes string not integ. What to do for this? Here, creating different object but using same instance variable. I want to pass _str=integ from first thread and from second thread _str=string.

Thanks:
Ramakrishna k.C
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
No, args[0] is not an Integer, nor an integer. It is a String.
Here args is a 2‑element String[]. Its first element is not 123 but "123".

If you want to pass different types you can overload the constructor, or you can give them the type Object, in which case an integer would be boxed to an Integer. That sounds like poor design, however, because you are abandoning type‑safety.
Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 220
So, according to java two different object created, but same instance variable. right? I can't overload constructor. Because if I want to pass more args, then I've to do that much overloading.
sounds like poor design, however, because you are abandoning type‑safety.

Dint get this.

Thanks:
Ramakrishna K.C
Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 220
Can I use Ehcache or memcached for this? I think its a good solution.

Thanks:
Ramakrishna K.C
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39530
    
  27
Why do you think that? What problem are you trying to solve?


Ping & DNS - updated with new look and Ping home screen widget
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3932
    
  17

I think you need to step back and explain, with a working example which shows the problem (read SSCCE.org). The code you posted doesn't behave the way you say. It doesn't show the entire story, so I made up a bunch to fill in the gaps:

Note I renamed classes and added output so you can see what is being executed - I used the Thread constructor which takes a Runnable and a name so I can name the Thread after the value it should be using, then I output the value and the name of the thread to make sure each thread uses the correct value. I also added a few sleeps in there to make the threads take longer to give the main thread a chance to change values.

Output of a couple of runs:
steve@Sluburntu:~/Documents/java$ java examples.Main integ string
STR is: integ in Thread integ
STR is: string in Thread string
steve@Sluburntu:~/Documents/java$ java examples.Main integ string
STR is: string in Thread string
STR is: integ in Thread integ

The value is always associated with the correct thread. Sometimes the first argument gets printed first, sometimes the second argument gets printed first, but never does the second argument replace the first in the first thread.


Steve
Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 220
Actually while crawling web-sites, I'm creating threads (say 2 Thread managers and 5 workers threads for each managers). Firstly These Manager-threads calls another class called Write.Class to write a crawled page.




In Write.class, I've a method called WriteToFile(). So, when 1st thread-Manager enters to the method(WriteToFile()) and writing a file, The second manager enters to the same method i.e WriteToFile(). But, these managers accessing the same method with different instance, I thought new variables will be created in each object. But, It is not. Variables are changing. I can't put synchronized keyword for this. Because Manager-thread takes too much time to write file. If I pt synchronized, It fails the multi-threading crawling.

Thanks:
Ramakrishna K.C
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39530
    
  27
If this was my problem, I'd use only a single thread that writes to the file system; only once it's been shown that more are necessary would I introduce more. The worker threads that perform the download can push all necessary information (encapsulated in some object) into a LinkedBlockingQueue or ConcurrentLinkedQueue from which the writer thread removes it.

Or, if you want to get fancy, use a ThreadPoolExecutor and be thus prepared for any number of writers.
Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 220
@Ulf Dittmer

your answer is not entering my bullet-proofed skull. Bit more explanation please. I din't understand completely.

Thanks:
Ramakrishna K.C
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39530
    
  27
What did you not understand about it (forget about the ThreadPoolExecutor for now, that can come later if necessary) ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7015
    
  16

Ramakrishna Udupa wrote:Actually while crawling web-sites, I'm creating threads (say 2 Thread managers and 5 workers threads for each managers).

I think you're overthinking this. And Ulf's post just confirms what I was thinking.

It would appear that you've decided that multi-threading is going to solve all your problems before you've worked out exactly what it is you need to do - ie, you've decided the HOW before the WHAT - and that's not generally a good way to do things.

My suggestion: Forget all about threads and pools and managers, and work out your solution on a single-threaded model first. Then if (and ONLY if) you can prove that multi-threading is going to vastly improve your throughput, look at where you might employ it (and you'll probably discover that there are portions of the problem that lend themselves to it, not the whole thing).

And BTW, I totally agree with Ulf, the file I/O portion probably shouldn't be threaded (or, if it is, it should probably have a single, dedicated Thread).

Threading is complex, so don't use it unless you have to.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3932
    
  17

Ramakrishna Udupa wrote:... I've a method called WriteToFile(). So, when 1st thread-Manager enters to the method(WriteToFile()) and writing a file, The second manager enters to the same method i.e WriteToFile(). But, these managers accessing the same method with different instance, I thought new variables will be created in each object. But, It is not. Variables are changing.

Then you have screwed up your scope, are mis-reading your code, or mis-reading the results.

If the variables are method-local variables (which you should make every effort to use), then there is no chance that the variables are being mixed (which is why you should make every effort to use method local variables).
If the variables are instance variables, then they will not be shared between instances. If you see them being shared between threads it means you are using the same instance (same Object) in multiple threads.
If the variables are static variables, then they will be shared between all instances, and you have to copy locally and use synchronization to keep them separate. Of course, if you want to keep them isolated then you shouldn't make the variables static.

Remember the same is true for the data your variables hold. If you have a Write object and that Write object is different for each thread, but it uses an array and you pass the same array to all instances of Write, then you will get the same data in all instances of Write and in all Threads.

In the end, if you don't provide a small SSCCE (<- link), it will be hard to help.

But, like Ulf, I would suggest using a single thread to do all your writing. A disk can only write in one place at a time. If you are writing to different files on the same disk in multiple threads then writing may take much longer, and your files may become fragmented. If you are writing to the same file from multiple threads then it is possible the data will be corrected or data race conditions occur unless very careful about synchronization.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating two Threads using same resource will affect the control flow
 
Similar Threads
Thread synchronization doubt
about notify() and wait()
How to pass value from one file to anaother
Why doesn't this code work in Netbeans 7.0?
static object initialization