dcsimg
How can I "smooth out" the order in which...
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ryan_Hope
Posted On:   Monday, February 26, 2001 11:59 AM

How can I "smooth out" the order in which threads get executed? I am having problems with thread scheduling. I have a bunch of threads, which get some values from a socket, then write them to a database. All of the thread share a single connection to the database. The doQuery() method that writes to the database is synchronized. Some of the threads are allowed to execute the synchronized doQuery() many many times in a row while others wait, even though all of their priorities are the same. With 4 concurrent threads, it's not uncommon for one of the threads to wait for 50 lock-unlock cycles before being allowed to execute the doQuery() once. Is there some way to "smooth out"   More>>

How can I "smooth out" the order in which threads get executed?



I am having problems with thread scheduling. I have a bunch of threads, which get some values from a socket, then write them to a database. All of the thread share a single connection to the database. The doQuery() method that writes to the database is synchronized.



Some of the threads are allowed to execute the synchronized doQuery() many many times in a row while others wait, even though all of their priorities are the same. With 4 concurrent threads, it's not uncommon for one of the threads to wait for 50 lock-unlock cycles before being allowed to execute the doQuery() once.



Is there some way to "smooth out" the way in which the threads are chosen for execution? From everything I've read, it's not possible without implementing my own locking objects.



Here's the version I'm using...



%> java -version

java version "1.2.2"

Solaris VM (build Solaris_JDK_1.2.2_06, native threads, sunwjit)

   <<Less

Re: How can I &quot;smooth out&quot; the order in which...

Posted By:   Finlay_McWalter  
Posted On:   Tuesday, June 12, 2001 06:39 PM

I'm assuming that the only synchronisation between the threads is the monitor that protects doQuery()


If your program is written to look
something like this:



doStuff(){
while(true){
getJobFromSomewhere();
doQuery(); // synchronized
reportResult();
}
}


Then it will (potentially) service all the queries for one thread before allowing another to run. Instead, put a yield() into the loop to allow others that are pending to run:




doStuff(){
while(true){
getJobFromSomewhere();
doQuery(); // synchronized
reportResult();
Thread.yield(); // ADDED
}
}
About | Sitemap | Contact