27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Threads<br />

sleep(10000) says that it just can sleep 10 sec if nobody needs it. That is reason why<br />

you do<br />

try{<br />

sleep(10000);<br />

}<br />

catch(InterruptedException x){<br />

//do smth here<br />

}<br />

You just try, it is not an order. It tries but not always sleeps!<br />

Hi, Is there somebody who can tell me why my thread sleeps longer then I told<br />

him to do...<br />

I have a thread that has to sleep for 60000 millesec. But every 4, 5 minutes<br />

it sleeps for 61000 millesec.? I have to built an application that get the<br />

time every minute, but with this sleep I can't trust the java threads.<br />

So can somebody tell me what is going wrong???<br />

Answer: Really JDK never give you warranty that will wake your thread after XXX<br />

ms.<br />

You can be sure only<br />

that your thread will not be waked up before!<br />

For good timing you should take another, better for real time perfomance, VM.<br />

For example PERC from Nemonics.com or something else...<br />

I have created a program with a main method that instantiates and starts three<br />

threads, the first two of which are daemons. Why daemons does die when normal<br />

thread die?<br />

Answer: Because of nature of daemon threads. They are alive if exists at least one<br />

"normal user's" thread. Otherwise they die immediately<br />

Q: Does anyone know if there is a way for two threads to find each other if they<br />

are started in two different JVM?<br />

In other words, I start a thread in one JVM, then I want to PipeWrite to another<br />

Thread that is doing a PipeReader. Any help would be appreciated!<br />

Answer: Use of Piped streams is only supported inside the same JVM. If you want<br />

IPC, you have to use sockets or xfer data using files. I would recommend the sockets<br />

approach, it has the added advantage of working not only between process, but<br />

across the network as well.<br />

Use of other IPC mechanisms, like shared memory, pipes, mail boxes, etc, are not<br />

supported by the JVM core. You could always roll your own native code, but why<br />

bother when you have access to sockets?<br />

RMI is another possibility but it all depends on what you want to do. I'll let the RMI<br />

experts talk about this option.<br />

And CORBA, if you want to get real fancy.<br />

--<br />

file:///F|/350_t/350_tips/threads.htm (2 of 4) [2002-02-27 21:19:25]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!