• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton and Threading in EJB

 
Scott Seo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this month's JavaRanch article on the Distributed Cache pattern.
Kyle
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic