• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Singleton and Threading in EJB

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this month's JavaRanch article on the Distributed Cache pattern.
Kyle
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic