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 Need to understand about threading issues? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Need to understand about threading issues?" Watch "Need to understand about threading issues?" New topic
Author

Need to understand about threading issues?

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
I know sometimes, the thread can step up on each other. But when this scenario is possible.

This is what i understood, if threads step up on each other-

1) they will start sharing the instance variables. I mean to say that instance variables will behave like static variables.

How to detect this, that this code possess potential, and the threads are going to step up each other?


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
What you mean by Step up??
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Moving to the threads forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Pratap koritala wrote:What you mean by Step up??

it do means, threads coincide with each other.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Amandeep Singh wrote:they will start sharing the instance variables. I mean to say that instance variables will behave like static variables.


This may be simplifying things. Whenever you have an Object that is shared between multiple threads, all of its instance variables will be shared between the threads - all the time. This has to be something to be watched for, and if you don't want it to happen then you need to use method-local variables or ThreadLocal objects.

Things really get problematic when you have multiple threads accessing the same variable at the same time (either because they access the same code or because they access different code that use the same variables). This is a worse problem than just 'sharing the data between threads' because it can make any given method behave incorrectly, or it can actually break your variable's data (storing gibberish, inconsistent data, Maps that aren't mapped properly, things like that).

Your question was 'how do you detect it'? The short answer is 'if you have multi-threaded code, you should assume the Threads will interfere with each other, and protect your code accordingly.' The medium answer is 'you can usually detect this sort of thing with a well-written test suite that runs concurrent tasks and checks all tasks for consistency - and is run often.' These problems are often rare events but when they occur they can kill an app. So you can't just test a few times it has to be tested for long durations under heavy stress.

The best thing to do though (along with assuming there IS a problem and thoroughly testing concurrent actions), is to go through your code and search for code that may mis-behave
1) What classes are used in multiple Threads?
2) Of those classes, what non-final instance variables are used in the threaded code?
3) Of those variables, which ones can be localized to methods, and parameters? (if any are instance variables only for convenience in passing between multiple methods, then make them parameters instead)
4) Of the leftover variables, do you properly protect their access and writing?
-- Which can be done a number of ways which is often dependent on how the variable is used. For example, using some of the collections found in java.util.concurrent instead of normal List, Set, Queue, or Map implementations, using volatile or Atomic variables for single-step reads or writes, using synchronization to make sure multi-step reads/writes are performed atomically (means: as a single unit which prevents other threads from mangling the variables while this thread still is working or from reading inconsistent values), or using ThreadLocal objects to hold data that each thread must have but should not be shared by all threads.

Steve
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Very nice explanation. Excellent.

1 last question i do have is- i do see my legacy java code.

example-

this is the code, how do i know that this is called by mutliple threads ?
using the class constructor, i can know it is loaded only 1 time.

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
I think i can use the following method to know about the current thread count.

Thread.activeCount();

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
3) Of those variables, which ones can be localized to methods, and parameters? (if any are instance variables only for convenience in passing between multiple methods, then make them parameters instead)


Is this sure, the local method level variables are safe, when the same method can be accessed by any of 48 active threads ?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Amandeep Singh wrote:
3) Of those variables, which ones can be localized to methods, and parameters? (if any are instance variables only for convenience in passing between multiple methods, then make them parameters instead)


Is this sure, the local method level variables are safe, when the same method can be accessed by any of 48 active threads ?


Yes. Local method variables are stored on the stack and each thread has its own stack. So it doesn't matter how many threads are accessing the same code, they each have their own independent value.

You ask: "How do I know if the code is running in multiple threads?"

You have to review your code and its environment to know when Threads are used and how they get used, where your objects get created, and where they get passed around. If for some reason you can't figure that out then you have a couple of options:
1) Add debugging code that reports Thread.currentThread().getName(). You should add it to all your methods, because some methods may be called from one Thread and other methods from a different Thread. If you get different names for the same Object, then you know the Object is used in multiple Threads.
2) Use a profiler as you run the code to see what threads are active and what objects they are accessing
3) Assume any object you don't know for sure about IS being used by multiple threads and act accordingly.

Thread.activeCount() will tell you the count of all active threads, not just the ones using a specific object. Every Java app will have multiple threads, and some of them don't affect the application's behavior at all (things like the garbage collecting thread for example). So it isn't really a reliable number to base your decisions on. You need to know the environment the code is running in to know how threads affect the application.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
I used your first approach to see the thread name's.
All thread name's are same but they have different number appended to their common name's.



Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Amandeep Singh wrote:I used your first approach to see the thread name's.
All thread name's are same but they have different number appended to their common name's.



I don't know how to read that log (I don't know what was logged), but from this description, it sounds like each one is run with a different Thread. Threads created from a Thread Pool often have the same name (from the common ThreadFactory or ThreadPool) and different numbers appended to the end. This sounds like what you are getting.

It also looks like you have a server, perhaps a JEE server? If this is the case the problem gets even stickier, because concurrent requests will each be run in their own thread, but often Threads will be reused for other non-concurrent requests later on. This means that ThreadLocal is NOT a safe means of storing data, if the data needs to be stored for a particular request or user only. If this is the case, then you would need to make NEW objects in the proper scope (for example one object for each user session stored in the user's sessions, one for each request stored in the request scope rather than one object and using synchronization or ThreadLocal variables to make sure values are maintained properly), and be very careful that NO object gets stored in a wider scope than is necessary (don't put anything in the application scope that holds user or request specific data, for example).
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
It is definitely a J2EE server. But it is not a struts or jsp application. The front end is html/javascript and the backend is pure Java.

I agree with you, a common thread factory is being used.

When you say ThreadLocal is not a safe means to store data. It still means that the local method variables are still safe. I know ThreadLocal variables are different from local variables.

I am surprised with your so much indepth knowledge.

Really you are genius.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Amandeep Singh wrote:
When you say ThreadLocal is not a safe means to store data. It still means that the local method variables are still safe. I know ThreadLocal variables are different from local variables.


Yeah, method local variables are still thread safe, and are the best means of shuttling data around in general. But the ability to do so properly is limited in a web application so you have to be aware of the proper scope of data.


I am surprised with your so much indepth knowledge.

Really you are genius.


Haha. No genius here :+) Just some experience with a lot of mistakes which I hope I can help others avoid.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to understand about threading issues?
 
Similar Threads
threading single instance vs multiple instances
Java class instances question
Is there any alternative for synchronization?
does find(String[] criterion) need to be synchronized ?
shared count variables for instances