wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Java Threads for cuncurrency Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Java Threads for cuncurrency" Watch "Java Threads for cuncurrency" New topic
Author

Java Threads for cuncurrency

Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108

Hi Friends,

Its a long time since I have logged into this site.

I have a singleton object which has a method containing some parameters (external systems credentails). The singleton object tries to connect to external systems based on the arguments passed. one of them gets connected and the rest fails. Now currently I am doing it in a sequential way. i.e first try to send the details to the singleton object and see the response. If its not connected then try with different arguments until the singleton object establishes the connection for which i know the credentials. Can i do it using threads? so that i need not have to wait for n attempts before getting the right one.

If yes.Is it equivalent to have a parallel process running. Are there any risks like deadlock etc.

Regards,
Pradeep


Thanks and Regards, Pradeep Kumar
SCJP 1.6, SCWCD 5.0
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Pradeep Kumar wrote: Are there any risks like deadlock etc.


Yes.

Why are you using a singleton?

Why are you having multiple threads access the external resource?
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Hi Pradeep Kumar,

There are various ways to do this. First things first - why the object is singleton? I mean, you don't have to give me the answer, but you should be knowing it. Because, if that class is not singleton, then it is quite easy to go for multithreading:
1) Create 10 objects (assuming that there are 10 external systems)
2) In a Thread (or Runnable) you'll write a run method which will take inputs like object with details, id of the external system etc.
3) The moment one thread gets success, it signals other threads (e.g. modifying a static variable etc.) so that other threads quit.

This is not parallel processing. Here, we are running threads.

If no thread is waiting for another thread, I don't think that it will lead to deadlock.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108
Anayonkar Shivalkar wrote:
This is not parallel processing. Here, we are running threads.
I hope this helps.


The class was a singleton because it was a utility class and will be used throughout the application to connect to external system. My question is if it is singleton, how is it an issue? since we are creating threads, and threads can operate on a singleton object. a thread can notify other threads when one of the thread succeeds.

Also if we create threads and run on a singleton object why is is not a parallel process. Yes i agree, that it ups to the scheduler to pick which thread to run but we can assume that threads all threads are sharing the CPU and hence assume its running in parallele

Please comment on the same.

Regards,
Pradeep
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Pradeep Kumar wrote:My question is if it is singleton, how is it an issue?

Ok. Let's assume that class is not singleton. Now, what you are gonna do is : create different objects of that class - each object will carry configuration of one external system, and provide one object per thread. So that you'll attempt different connections at the same time.

But, now your class is singleton, and hence you cannot create more than one objects. So, yes, you can provide same object to different threads, but basically, you are asking different threads to connect to same system. Is there any way so that the one object could hold multiple configurations for external systems? e.g. list of configurations etc.?

Pradeep Kumar wrote:Also if we create threads and run on a singleton object why is is not a parallel process.

Because, all threads run under same process, so, it is multithreading, not multiprocessing.

I hope this helps.
Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108
Anayonkar Shivalkar wrote:
Pradeep Kumar wrote:My question is if it is singleton, how is it an issue?

Ok. Let's assume that class is not singleton. Now, what you are gonna do is : create different objects of that class - each object will carry configuration of one external system, and provide one object per thread. So that you'll attempt different connections at the same time.


Pradeep Kumar wrote:Also if we create threads and run on a singleton object why is is not a parallel process.

Because, all threads run under same process, so, it is multithreading, not multiprocessing.

I hope this helps.


Yes i got the difference you mentioned with the singelton that you were referring to.


What I am still not clear is if its not a singleton and we create n threads and n number of objects for n configurations, how is this better than looping in a for loop and testing each configuration at a time inside a for loop without the thread concept. What is the advantages?

Please let me know your thoughts on the same.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Pradeep Kumar wrote:The class was a singleton because it was a utility class and will be used throughout the application to connect to external system. My question is if it is singleton, how is it an issue? since we are creating threads, and threads can operate on a singleton object. a thread can notify other threads when one of the thread succeeds.


This is a terrible reason to use a singleton. And IMHO, singletons are terrible in and of themselves. Singletons cause problems.

If there are multiple threads operating on an object, singleton or not, then you need to be extremely careful with locks and synchronization. Proper design and implementation of concurrency in Java is possible, but it is very tedious and error prone. This is one of the reasons that I am looking to use Scala or other languages that do not require PhD level skills to write good concurrent code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Threads for cuncurrency