| Author |
Singleton and Threading in EJB
|
Scott Seo
Greenhorn
Joined: Nov 24, 2003
Posts: 6
|
|
I have an application that I have been working on for the past year. It has mulple threads which may or may not open telnet session to a same ip address. I am using singleton as a central registry to block a tread from opening a telnet session to an ip address which may be in use by another telnet session spawned by another thread. I am trying to port over some of the business logic to EJB. How would you address singleton and concurrency issues I have in EJB?
|
 |
Vijaykumar Dafal
Greenhorn
Joined: Sep 27, 2003
Posts: 1
|
|
Hope you might find the below code snippet useful: Note that you have to synchronize an object public class Singleton { private static Singleton m_Singleton = null; public static int i_Vallue=0; private Singleton() { } public Singleton getInstance() { synchronized(m_Singleton) { if(m_Singleton == null) { m_Singleton = new Singleton(); } } return m_Singleton; } public static void showValue() { System.out.println(i_Vallue++); } public static void main(String[] args) { new Singleton().showValue(); new Singleton().showValue(); new Singleton().showValue(); Singleton.showValue(); } } [ November 27, 2003: Message edited by: Vijaykumar Dafal ]
|
 |
Scott Seo
Greenhorn
Joined: Nov 24, 2003
Posts: 6
|
|
|
Thanks for the snippet but when you have multiple JVMs such as mutiple EJB servers across mutilple servers, how can you synchrozied on an object?
|
 |
Kyle Brown
author
Ranch Hand
Joined: Aug 10, 2001
Posts: 3879
|
|
Read this month's JavaRanch article on the Distributed Cache pattern. Kyle
|
Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
|
 |
 |
|
|
subject: Singleton and Threading in EJB
|
|
|