aspose file tools*
The moose likes Threads and Synchronization and the fly likes Is using While(true) loop better way to keep threads on? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Is using While(true) loop better way to keep threads on?" Watch "Is using While(true) loop better way to keep threads on?" New topic
Author

Is using While(true) loop better way to keep threads on?

Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 378
1) Is while loop better way to keep threads on, on fly? or should we use another concept?

2) Every thread is doing tremendous work of connection with database, making XML's, Logs, processing etc. and independent of each other thread

3) A parent thread which born all child threads, need to track all Childs too, so is it good to use the above and do this?


Regards
Azrael Noor
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Azrael Noor wrote:1) Is while loop better way to keep threads on, on fly? or should we use another concept?


It's not really clear what you're asking, but if you're talking about a spin lock, then, no, that's a horrible approach. Can you clarify your question?

Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 378
Jeff, like i am taking following example:






Like i call run() five times and five thread are made and five thread are independent and doing all above mentioned activities. Thread is in while loop and this is happening in Enterprise application.

is this fine? Any alternative to this ? if it is horrible then why we say it is?

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

That structure is fine in general. However, you probably don't want to create a new Thread each time through the loop. Depending on how quickly your input is arriving, you may end up creating thousands of threads and running out of resources. You won't be able to process thousands of things concurrently anyway, so there's no point in that.

If you're going to do the work in separate threads, a more reasonable approach would be to use a Producer/Consumer model, with a work queue between the Producers and the Consumers. Use a thread pool for the Consumers that do the processing. You probably don't want more than 5-10 threads in the pool, but you can make that number a configuration parameter and tweak it during your testing to find out what works best for your situation.
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 378
thank you Jeff


4 - 5, Thread is created once on beginning of Application and keep on looping. (in one module of app)

and for 6 modules around 13 - 15 Threads running in system and all through same procedure while(true) without effecting each other.

Is this spinlock as i have read:

The longer a lock is held by a thread, the greater the risk that it will be interrupted by the OS scheduler while holding the lock.

> Will it stop some thread in near future in enterprise application which have dedicated server?

> I am unaware of producer consumer model functionality, could you provide me any link regarding these?



Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 378
I have searched around mostly people are fine with this paradigm, some are saying to follow:



or

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Azrael Noor wrote:
Is this spinlock as i have read:

The longer a lock is held by a thread, the greater the risk that it will be interrupted by the OS scheduler while holding the lock.


No, a spin lock is when you do something like this:


instead of using the proper tools to get a thread to pause. It's a huge waste of CPU, and absolutely the wrong way to introduce a delay. It's what I thought you were talking about with your original post, but I see now that I misunderstood.

> I am unaware of producer consumer model functionality, could you provide me any link regarding these?


A google search will turn up plenty of results. I don't know which ones are good, so you'll have to look at a few until you find something that makes sense and that you understand. Stay away from roseindia though. That site is horrible.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Azrael Noor wrote:I have searched around mostly people are fine with this paradigm, some are saying to follow:



or



No, don't do that. Only sleep if you actually want the thread to pause for a fixed amount of time. If you're waiting for some other event in the system, use wait() or join() or the higher level tools in java.util.concurrent.

The second one is utterly pointless.
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 378

Still you put much light on it

** I was told to use Timer and TimerTask, but i have no reason why to use them, if i have while(true) and i need continue pinging to database to check for requests to process further.

** Yup Rose India is an Indian Site

Still i got an idea that while(true) is not bad practice here , but i have seen sometimes thread stop after a month or two. i felt that this could be reason and spin lock may be there as you said.... it is not so issue may be some other




Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Azrael Noor wrote:
Still you put much light on it

** I was told to use Timer and TimerTask, but i have no reason why to use them, if i have while(true) and i need continue pinging to database to check for requests to process further.


It depends on your needs. If you want to process data as fast as it comes in, and you expect it to keep coming in forever, then


is the way to do it.

On the other hand, if you want to process data periodically--every 5 seconds or every 2 minutes or something like that--then you can use Timer and TimerTask.

Still i got an idea that while(true) is not bad practice here


It's fine, as long as you're doing actual processing in the loop. If you're just using it as a delay, that's wrong.

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3970
    
  17

Jeff Verdegan wrote:
Azrael Noor wrote:

No, don't do that. Only sleep if you actually want the thread to pause for a fixed amount of time. If you're waiting for some other event in the system, use wait() or join() or the higher level tools in java.util.concurrent.


I disagree here, as long a the system.out.println("here 1"); represents real code. The reason you check for interruption in the while() statement is to provide a means of ending the thread in a graceful manner. At any point in your task, even during sleep() another thread can signal this one to stop at an opportune time by calling interrupt() on it. I usually do this with a boolean signal so I know when the interruption was intended as a shutdown signal rather than spurious. Something like:


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3970
    
  17

Me wrote: I usually do this...

Actually, I am not sure I remember ever doing this with sleep... but I do it with basically anything else which can be interrupted. So maybe Jeff's concern is more specific about the use of sleep() in this construct.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Steve Luke wrote:I disagree here, as long a the system.out.println("here 1"); represents real code.


Right. I was thinking he was using it as a spin lock.

I'm not thrilled about using thread interruption to signal that we're done processing, but if we're calling methods that can throw InterruptedException, then we have to deal with it anyway.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Steve Luke wrote:
Me wrote: I usually do this...

Actually, I am not sure I remember ever doing this with sleep... but I do it with basically anything else which can be interrupted. So maybe Jeff's concern is more specific about the use of sleep() in this construct.


I was probably multitasking when I responded and didn't really grok what he was trying to illustrate, but I think my concerns were a) spin lock, and b) using sleep() as an arbitrary slow down or take-turns construct.
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 378
I wonder if i do not give any SLEEP to thread and continue process it, what is issue with that.

Normally when thread does not do any heavy activity, It just open database connection and poll that he is living, thats all.

I do not require Interruption in Thread of any form.

I want the Thread should run for a year successfully
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Azrael Noor wrote:I wonder if i do not give any SLEEP to thread and continue process it, what is issue with that.


No. The only reason to use sleep is when your business logic requires a thread to pause for a fixed time, such as if you're polling something on a time-based period or checking on an external resource where you can't use the other language and API tools to wait for it. You don't use it to give other threads processing time or anything like that.

Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 378
actually all 12 threads are polling one database and single table to update their respective status and to pickand process request and almost every thread sleep for 5000 ms.

so as you said we should use sleep else database will kick :P
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Azrael Noor wrote:actually all 12 threads are polling one database and single table to update their respective status and to pickand process request and almost every thread sleep for 5000 ms.


What do you mean when you say the threads "sleep fro 5000 ms"? Do you mean there is 5s of inactivity while the thread blocks waiting for the DB to return? If so, that's not really "sleeping."

Or do you mean you have added sleep(5000) calls? If so, this is only a correct approach if your business logic calls for polling the DB every 5 seconds.

so as you said we should use sleep else database will kick :P


I don't know what you're talking about here.
 
 
subject: Is using While(true) loop better way to keep threads on?
 
Similar Threads
Thread creation location
Threads - stopping a while loop
Understanding wait() and notify()
the end of thread
Threads Completed Notification