Re: The simplest way to download a file from http resource that need
authentication
Andrea Francia wrote:
Lew wrote:
Andrea Francia wrote:
Authenticator.setDefault() method which is a static method and therefore
not usable in a threaded enviroment.
Static methods can be used in a multi-threaded program.
Nooo, really?
Yes, really.
There is a race conditions.
....
The correcteness of the programs depend of the scheduling, hence there
is a race condition. The race conditions depends from the fact that
Authenticator.setDefault() is static and hence the same data is shared
by all threads.
So?
Non-static methods can have race conditions, too. Deadlocks, even. There's
no difference from static methods in that regard. Why do you single out
static methods?
Fortunately Java has a number of lovely built-in constructs to keep threads
synchronized, starting with the keyword "synchronized", which are notably
absent from the example you posted.
Of course if you don't synchronize your threads, there will be trouble, static
or non-static methods notwithstanding. If you think using instance methods
without synchronization will solve your threading problems, you're doomed.
For an introduction to the topic, read
<http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html>
--
Lew