-
Help in ThreadLocal
help please i'm quite new too java. Here's the code what's wrong with these?
the idea is that there is a singleton Calculator object, with a single ThreadLocal object used to hold the total.
Please help.
**
* Simple class that can be used to total numbers.
*
* The class is normally used within the following framework.
* <pre>
* Calculator c = Calculator.getCalculator() ;
* try {
* c.start() ;
*
* c.add( ... ) ; // call the add function as many times as necessary
*
* result = c.getTotal() ;
* } finally {
* c.stop() ;
* }
*</pre>
*
* This class is currently not thread safe since the member field
* total is common to all threads. Use the class ThreadLocal so that
* multiple threads can use the calculator simultaneously.
*
* Do not change any public signatures or interfaces otherwise
* the test application will break.
*/
public class Calculator {
private int total ;
private static ThreadLocal calculator = new ThreadLocal() ;
private Calculator() {
}
public Calculator getCalculator() {
return(Calculator)calculator.get() ;
}
public void start() {
total = 0 ;
}
public void add(int value) {
total += value ;
}
public int getTotal() {
return total;
}
public void stop() {
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks