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 all threads die when after 2 hours when cpu reaches 100% Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "all threads die when after 2 hours when cpu reaches 100%" Watch "all threads die when after 2 hours when cpu reaches 100%" New topic
Author

all threads die when after 2 hours when cpu reaches 100%

ayman elgharabawy
Greenhorn

Joined: Sep 12, 2002
Posts: 19
i have application to run threads and each thread connecting to diffrent database and make some process in a schadual task .

the problem is all the theads hangs when the cpu is 100 % utilies and this happend not imedatly but just after certine time like 3 -4 hours or may be 10 hours
and this is my run method
public void run() {



while(true){
try{
Thread thread = Thread.currentThread();


synchronized(thread)
{

Process();
this.sleep(this.period);
}catch(Exception eee){
eee.printStackTrace();
return;
}

}
}





Ayman Elgharabawy<br /> <br />SCJP1.4
ayman elgharabawy
Greenhorn

Joined: Sep 12, 2002
Posts: 19
No Body has a advice for my problem

i will explain again i have multi threading applicatin and i have to put it in a very loaded server and this server some times the cpu reaches 100%usages and this makes my program stop all threads and die
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Originally posted by ayman elgharabawy:
No Body has a advice for my problem


I understand your frustration, but we here at the JavaRanch are volunteers, so questions get answered when someone gets around to it. You really aren't giving us much to go on, so everyone moves on to the questions that are better documented and easier to answer.
Looking at the code you gave us, it is clear that you don't understand multithreading well (you don't need a "synchronized" block in your run() method because there should only be one thread in that method). Check out the
Java Tutorial: Threads chapter for the basics.
It sounds like you are trying to write a scheduler. Why reinvent the wheel when the API has two: javax.swing.Timer java.util.Timer
Look at them and see if they can get you closer to your goal.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
See if I'm getting the problem ...

You have some number of threads that you start at server startup.

Each thread monitors one database. It processes any outstanding work it finds in the database, then sleeps, then repeats.

CPU usage grows over time, finally hits 100% and things stop working.

Does memory usage grow, too?

Sounds like something is running away and never stopping. Do you start new threads over time? Do you get floods of new tasks on the database at some point?

Any chance you're on JDK 5? There's a neat Consolethat lets you monitor threads and memory and such remotely.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
ayman elgharabawy
Greenhorn

Joined: Sep 12, 2002
Posts: 19
ok guys, the cpu usage is 100% but the memory usage is not
and i tried java.util.Timer and timerTask and i got the same results
if any body wants me to send him the code or try my program i have no problem
i want to know how to make the schadual task robust enough even if the server is too loaded or guarantee the thread never dies

here is part of my code after some summary


public class easythread extends Thread{

public static void main(String [] args)
{
for(int i=0;i<5;i++){
easythread et= new easythread ();
}

public void run() {
while(true){
Process();
this.sleep(this.period);

}

}

}
ayman elgharabawy
Greenhorn

Joined: Sep 12, 2002
Posts: 19
i have also to mention that the program doesnt use much cpu time
plus he cpu doesnt increase but i have already loaded cpu even befor my program run!!!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Originally posted by ayman elgharabawy:

i want to know how to make the schadual task robust enough even if the server is too loaded or guarantee the thread never dies


Can't be done. If you write bogus enough code you can crash the most hardened system. If your system is already at 100% CPU use, I'd say that may be your problem right there. Upgrade your hardware to something that can take the load.
As for the code you've shown us, you are still not telling us much. You may be doing something in the Process() method, like not closing database connections, which is causing your problem, but we'd never know. Do you get any exceptions?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Some systems will have accounting limits and will automatically kill a process that exceeds those limits. If you're on a UNIX platform, try running "ulimit" to see if there's a CPU usage limit.


[Jess in Action][AskingGoodQuestions]
ayman elgharabawy
Greenhorn

Joined: Sep 12, 2002
Posts: 19
i have no exception at all and i close the database connection and all the resultsets and statements

i think it is a java thread problem as i have vb programs doing the same tasks
and works fine even if the cpu is 100% usage
my server is win2000 and i couldnt change to linux

even if any body try to make any simple thread program and put it in this case
he will get the same result on windows..
and this is not robust at all!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: all threads die when after 2 hours when cpu reaches 100%