[Prévia] [Próxima] [Prévia por assunto] [Próxima por assunto]
[Índice cronológico] [Índice de assunto]

Re: Metodos static com synchronized



Para esclarecer melhor meu exemplo anterior..  
Gostaria de fazer uma class singleton que seja thread-safe.  Fiz assim:

class Singleton
{
   static Singleton instance;
   
   protected Singleton()
   {
   }
   
   public static Singleton getInstance()
   {
     synchronized(Singleton.class)
     {
       if ( instance == null )
       {
          instance = new Singleton();
       }
       return instance;
     }
   }
}

Isto garante que que se dois processos concorrentes chamarem getInstance
ambos vao receber uma referencia para a mesma instancia?


On Tue, Jun 04, 2002 at 12:30:53PM -0300, Christian Willy Asmussen wrote:
> 
> Alguem pode me dizer se isto esta certo?
> 
> Quero fazer metodos "static" de uma classe serem synchronized.
> 
> class Test 
> {
>   private static boolean lock = false;
>   
>   public static lock()
>   {
>     synchronized(Test.class)
>     {
>      lock = true;
>     }
>   }
>   
>   public static unlock()
>   {
>     synchronized(Test.class)
>     {
>      lock = false;
>     }
>   }
> }
> 
> 
> -- 
> There is no limit to what you can do
> if you don't care who gets the credit.
>  - Keynote

-- 
There is no limit to what you can do
if you don't care who gets the credit.
 - Keynote