• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating two Threads using same resource will affect the control flow

 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I use Ehcache or memcached for this? I think its a good solution.

Thanks:
Ramakrishna K.C
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think that? What problem are you trying to solve?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42970
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you not understand about it (forget about the ThreadPoolExecutor for now, that can come later if necessary) ?
 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!