473,473 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Threading. Is this an Issue?

Sup everyone!

I wrote this code for Tomcat appserver but I am told from an
associate that it has threading issues. The basic idea is to store a
read only data table for everyone to use. It takes some time for the
table to be created. I want to store the table in a singleton and
update it periodicly, basicaly when a user logs into the system. By
keeping 1 table allocated I don't have to worry about memmory usage
and if a user does need the latest updated table he can log off and
then back in to refresh the table(if nobody else has done so already).
If this sounds like a cludge maybe it is, but right now I question my
knowledge of threading. Functionality can change and maybe updating
the table is best in a worker thread that updates periodicaly on a
timed interval.

So here is the suspected threading issue:

I have a singleton, pretty basic class.

private constructor.
GetInstance
GetTable
SetTable

with a self and table class variable.

the table is created in a javabean that is accessed in the servlet.

A user accessing the table will
GetInstane().SetTable(javabean.createTable()) if
firstTimeAccessingTable == true else GetInstane().GetTable()

My associate says that the getters and setters are the problem. He
said that if thread1 does myTableRef = GetInstance().GetTable() and
uses the table while thread2 does
GetInstane().SetTable(javabean.createTable()) that thread1's
myTableRef will change once thread2 does the set.

So my sandBox test is not threaded but I think should be a good enough
example. In my test I tested a "dataType" even though I am not sure
one calls the String class a true dataType and a created class. My
result show that myTableRef will not change once the setTable happens
from another thread. Here is the Code.
public class Singleton {

static Singleton me = null;

private String testDataType = null;
private TestClass testClass = null;

private Singleton(){
}

public static Singleton getInstance(){
if(me==null){
me = new Singleton();
}
return me;
}

public String getDataType(){
return testDataType;
}

public void setDataType(String dataType){
this.testDataType = dataType;
}

public TestClass getTestClass(){
return testClass;
}

public void setTestClass(TestClass testClass){
this.testClass = testClass;
}

}

public class TestClass{

public int value = 0;

public TestClass(int value){
this.value = value;
}

public TestClass(){}

}

public class SandBox {
public static void main(String[] args) {

Singleton x = Singleton.getInstance();

String origDataType = "freaky1";
TestClass origTestClass = new TestClass (5);

x.setDataType(origDataType);
x.setTestClass(origTestClass);

String retrieved1DataType = x.getDataType();
TestClass retrieved1TestClass = x.getTestClass();

String newDataType = "freaky2";
TestClass newTestClass = new TestClass (10);

x.setDataType(newDataType);
x.setTestClass(newTestClass);

String retrieved2DataType = x.getDataType();
TestClass retrieved2TestClass = x.getTestClass();

System.out.println("woot!");
}
}

In Debug mode at the "woot" println I find that the object ID's for
retrieved1DataType and retrieved2DataType are different as well as the
TestClass counterpart.

This tells me my associate is wrong. But what do you think? Should I
test a multithreaded senerio? Do you think the setters and getters
are thread safe? Will a getters's reference be safe from another
threads setter?
Thanks a bunch!

Erik
Jul 17 '05 #1
0 1434

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Jane Austine | last post by:
As far as I know python's threading module models after Java's. However, I can't find something equivalent to Java's interrupt and isInterrupted methods, along with InterruptedException....
0
by: James R. Saker Jr. | last post by:
I've got a: "start server thread > Queue object, start server thread <> Queue object, start parsing client < Queue object" application that's got me puzzled. Probably an easy threads issue, but...
1
by: Ognjen Bezanov | last post by:
Hi, all Thanks all of you who helped me with the threading and queues issue. I am trying to get it working but I am having problems. When I try to run the following: cmddata =...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
3
by: ELO | last post by:
Hi all Every week, I need to get two files on a remote server. I have developped a C# Windows Service with two System.Threading.Timer to do this task For the first one, the delay (TimeSpan...
13
by: John | last post by:
I've got some reasonably complex business logic in my C# code, in a class called by a ASP.NET page. This takes around 3-4 seconds to execute. It's not dependent on SQL calls or anything like that....
4
by: JimD | last post by:
Is this safe? Any pitfalls? I have done threading in regular C# apps, but haven't had a needs to do threading in ASP.Net, until now. The issue I have ran into is this: Our corporate portal...
2
by: WXS | last post by:
When I see things in .NET 2.0 like obsoletion of suspend/resume because of the public reason MS gives of they think people are using them inappropriately.. use mutex, monitor and other...
0
by: R K | last post by:
I am delevoping a Scheduler module whose functionality is to load the Scheduled Item Details every day (For this I have created a System.Threading.TimerCallBack with Timespan 1 day) in a stack. Once...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.