Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Should I add a Thread.yield() ?  RSS feed

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method excerpt:

The aim of the method is to send the supplied Message via a socket.
Now the retryCount forces the code to retry invocations on connect().
The aggressiveness of the retries causes the connect() method to report
that it is connected when it is not. When I add a sleep duration of 700ms
right before the call to connect(); things behave correctly.

I want to know if adding a Thread.yield() before calling connect() would
achieve the same benefit I'm getting with a Thread.sleep(700).

Thanks.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might, but in general I doubt it. It's possible for a yield() to cause no delay whatsoever. It depends mostly on how many other threads there are competing for the processor's attention. I wouldn't rely on it to reliably produce a delay.

To be honest, that connect() method you're calling seems like a pretty poor, unreliable method. Seems like it should block until it connects, or throw an exception if it times out. I don't know how it works, but if it's under your control, I'd probably try to find a way to rewrite it or replace it.

But assuming you're stuck with the unreliable connect(): for something like this, I'd probably use sleep(), but modify it to start with a small delays and gradually increase the time. E.g.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The connect() method is implemented using apache mina, an NIO framework.
I'm a NIO newbie but I've found that MINA does things asynchronously.
When I connect (there is a connect timeout) to localhost on a certain port;
even if my server is NOT running, it would not throw an exception. But I
digress here. The client/server code is actually working now & I can transfer
data without any problems. I'm just trying to make small tweaks to improve
things. I'll take a look at the retry code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!