threadsafe 2 « Thread Safe « Java Thread Q&A

Home
Java Thread Q&A
1.concurrency
2.Development
3.Exception
4.Notify
5.Operation
6.Socket
7.State
8.synchronize
9.Thread Safe
10.ThreadPool
Java Thread Q&A » Thread Safe » threadsafe 2 

1. thread-safe methods    coderanch.com

Originally posted by Theo Jenks: Is a method inherently thread safe if it only accesses data local to that method (no member fields)? Any stateless class is inherently threadsafe, provided that the objects it accesses are either private to the thread, or threadsafe themselves. The method you gave is threadsafe, but no other thread must be modifying the collection the iterator ...

2. is ++x threadsafe?    coderanch.com

To all- I have a class that updates a counter in a static class that holds a bunch of counter variables. Multiple Message Driven Beans have the potential to update this particular counter using the pre-increment operator: ++x; A coworker has pointed to a reference in a book where if you do this: y = ++x; the assignment can be preempted ...

3. Is System.out.println(String) Thread safe?    coderanch.com

public class t { public static void main (final String[] args) throws Exception { final Object gate = new Object(); for ( int i = 0; i < 10; i++ ) { new Thread (new Runnable() { public void run() { try { synchronized (gate) { gate.wait(); } } catch (Exception ignore) {} for ( int i = 0; i < ...

4. what is being Thread Safe ??????    coderanch.com

Hi, I am a bit confused over what is being Thread Safe. Suppose I have a multi threaded program accessing a Hashtable, when thread A is accessing the hashtable, thread B can also access it and corrupt the data. Is this what is called not being Thread Safe ?? If I make the hashtable synchronized, that makes it Thread Safe, am ...

5. Question about Thread-safe    coderanch.com

6. StringBufferPool (why not Threadsafe) ?    coderanch.com

Hi, I made a StringBufferPool class with the idea to share the use of StringBuffers instead of recreating them each time. Here is the code public class StringBufferPool { private static final Vector freestack = new Vector() ; private StringBufferPool() { } /** * Get the first free instance of a string buffer, or create one * if there are no ...

7. Is my class thread safe? Help!    coderanch.com

8. JAVA API's Thread Safe?    coderanch.com

9. What do you mean my thread safe ??    coderanch.com

Originally posted by Saurabh Agrawal: Can anyone tell me what is the meaning of thread safe precisely ?? Imagine a Person object, with many member variables; among these are three variables that represent the street, city, and postal code where they live. You are writing a function which prints mailing address labels. Now, imagine that one person is moving to another ...

10. Is this thread safe?    coderanch.com

I have a static variable of type A A has a non-static method called B. B does not reference any static variables (naturally, because it won't compile if it did) but calls other non static methods which also do not reference any static variables. Is this thread safe? Meaning if two threads call method B, they will not corrupt each other's ...

11. thread safe    coderanch.com

Making an application "thread-safe" mostly applies to applications that use database tables. Thread-safe applications allow multiple users to use your application at the same time without one user updating a record,or deleting a record, if another user is in the middle of using that same record. Because the processor on a computer doesn't not really simultaneously work with two users (threads) ...

12. thread safe    coderanch.com

i am novice to j2ee technologies. i have been playing around with jsp and servlets and then i ran into this issue. i have a servlet and in that servlet i am creating an instance of a class (classA).i am calling some setters on that class. finally i am calling a method startThread() on that class. my classA implements runnable interface ...

13. Thread safe XML package?    coderanch.com

I'm currently porting an multithreaded application to Java. The application uses a thread safe XML package to ensure thread safe communication between threads. Since we are doing a "straight port", that means I need to find a thread safe XML package for Java, if possible. Are any of the XML packages available in Java thread safe? Which ones?

14. Is CountDownLatch Thread-Safe ?    coderanch.com

Yes and no.... Yes, the countdown latch is thread safe and can be used by multiple threads. But no, the value return from getCount() is only valid til right before it returns. If you need to have data consistency between methods, you need to synchronize externally. (And this is true whether the individual methods are synchronized or not) Henry [ May ...

15. Is this thread safe? Simple question.    coderanch.com

If the field is private, no other object can get at it, so it's safe. That's not quite right. The private field can be (and most likely is) modified from a public method (or from a private method that is called from a public method). Making it static makes it thread-unsafe, because other objects of the same class can get at ...

16. Is this "thread-safe"?    coderanch.com

public class UIDFactory { /** * Current time in milliseconds being used to generate the UID. Used to * generate UIDs without instantiating UIDFactory. */ private static StringTIMEMS= null; /** * Current ordinal to append to the current time. Used to generate UIDs * without instantiating UIDFactory. */ private static intORDINAL= 0; // Member variables private Stringprefix= null; private StringtimeMs= null; ...

17. What is a thread safe class?    coderanch.com

Hey, what a great hacker dictionery! Who knew that Yu-Shiang Whole Fish was hacker argot? Here's another one -- now you know one-banana problem n. At mainframe shops, where the computers have operators for routine administrivia, the programmers and hardware people tend to look down on the operators and claim that a trained monkey could do their job. It is frequently ...

18. tool to locate NON thread safe code?    coderanch.com

19. Is this thread safe?    coderanch.com

Is TestThread number thread safe in following example? class SomeService { public static void doIt(int number) { new TestThread(number).start(); } static class TestThread extends Thread { int number = -1; TestThread( int number ) { this.number = number; } public void run() { for( int i = 0; i < this.number; i++ ) out.write("-"); } } } class App { public ...

20. How to make a class thread safe    coderanch.com

The followup question is, of course, what if *not* all of the methods are atomic operations? For example... public class Student { private String name; public void setName(String name){ this.name = name; } public String getName(){ return name; } public void appendName(String name) { this.name = this.name + name; } } Then you have two choices. You can synchronize the access ...

21. What is Thread Safe program?    coderanch.com

22. Is this method thread safe?    coderanch.com

Hi, I have a simple question about threads. int methodA(int k) { k++; return k; } Is the above method thread safe?or is it necessary to add synchronized keyword to make itthread safe?I have a small confusion as this method deals with method variables only and not any instance variable.Please clarify. regards, Raja

23. threadsafe class requirements    coderanch.com

If you are sure that the object of the class that is under consideration will never have a chance to be accessed by multiple threads then synchronization would simply be overhead. Consider objects stored as a request attribute , you do not need to do anything as no other thread can access that object (Under normal conditions). In case you have ...

24. Thread safe classes    coderanch.com

Expanding on Edward's answers: 1. Making all methods synchronized is a common strategy for making thread-safe classes, but (a) making all methods synchronized does not necessarily guarantee thread safety, and (b) it's also possible to make thread-safe classes that do not use synchronization at all, and instead use other techniques (such as the classes in java.util.concurrent). 2. Sometimes, in poorly-designed not-really-thread-safe ...

25. is this class thread-safe ?    coderanch.com

am learning the thread in JDK1.5. The testing scenario is : we have 10 users and create 10 threads to authorize them. If authorized, put it into a ConcurrentHashMap, then the other threads don't authorize it again. Here is the coding. Questions: 1. do you think it is thread-safe ? 2. "static final" ConcurrentHashMap is necessary ? or "static" is enough ...

26. Class.forName Thread Safe ?    coderanch.com

I have the following code, it does a Class.forName based on the content-type of a particular attachment. So far so good.... I have few questions, Please answer at your convenience . String nameOfClass = MIMETYPEMAP.get(mimetype); if (nameOfClass == null) { if(mimetype.equals("APPLICATION/OCTET-STREAM") && (fileName != null)) { String filextension = getFileExtension(fileName); if (filextension != null) { nameOfClass = FILEEXTENSIONMAP.get(filextension); } else { ...

27. Class is Thread-Safe or not?    coderanch.com

Well it is thread-safe, but because is just doesn't do anything! Classes become unsafe when two different threads can interact with the class at the same time... one changing the classes data while the other is reading. That's when you need synchronization... to protect the class during changes, so that any references to the data are safe from being changed during ...

28. Is the following program thread safe ?. Please help...    coderanch.com

I've been trying to analyze the following class and see that there is always a dead lock in the following call to hello. Yep this is a multithreaded application. public final class ABCD { /** A HashMap to store the mappings between file and its extensions. **/ private static final Map < String, String > MIMETABLE = new HashMap < String, ...

29. vetor thread safe?    coderanch.com

It is, technically, in the sense that each method individually is thread-safe. However it's difficult to accomplish anything useful with this class without some additional synchronization. For example when iterating, you need to synchronize, as shown here. Yes, that discussion is for Collections.synchronizedList(), but Vector works the same way. The basic problem is that while each individual method call is synchronized, ...

30. thread-safe java beans    coderanch.com

You might want to look at Joshua Bloch's Builder pattern (a specific adaptation of the GoF Builder), discussed in Effective Java, 2nd Edition. You can also find a short version here on p. 7-10 of the PDF. Also places like here and here. One thing that none of these links really discuss is that to be properly immutable in a multithreaded ...

31. Are local objects thread-safe?    coderanch.com

I am working on a multi-threaded application where we have ensured that we avoid any static or instance variables (wherever possible) so that we do not need to synchronize (to improve performance). I was under the impression that local variables are always thread-safe (because these variables are created on the stack), but now I realize that only the object references created ...

32. Is this class thread safe    coderanch.com

Originally posted by Ulf Dittmer: The class has no state at all, so calling it immutable doesn't make much sense. But more importantly, just because the state of a class isn't mutable does NOT imply that it's thread-safe. It may call other classes that do something that's not thread-safe, and that's what's happening here.

33. Calling thread unsafe methods from thread safe method    coderanch.com

Hi! Having done most of my development work in a bean environment where I don't really have to worry too much about threads I now find myself doing some real thread work... So you don't have to worry about multi-threaded access to local variables, because local variables reside on the Java stack and each thread has it's own Java stack... Does ...

35. Is SAXParser in JDK6 thread-safe?    coderanch.com

36. Is this method thread-safe ?    coderanch.com

I have a class that is defined as an Enumerated Type, i.e. there are only 6 instances created. In order to interface cleanly with some legacy code, I want to add an instance method to this class similar to the one shown below. The instance method does NOT change the class in any way; it serves merely to make a comparison ...

37. thread safe class    coderanch.com

38. How to find out whether given method is thread safe or not?    coderanch.com

I am bit confused. I am using Google SOAP for searching on Google. I want to search using multiple threads and want to add thread pooling concept in my application. I am using doSearch () of GoogleSearch class to search on Google. I want to know, is doSearch () thread safe? How to find out whether given method is thread safe ...

39. thread safe    coderanch.com

Because they are synchronized. Therefore if more than one thread is going after a vector (or hashtable) at the same time, the first one has to complete it's action before the next one can start. This prevents things from getting scrambled, thereby saving poor innocent programmers from having headaches. The down side is that synchronized code is about 6 times slower ...

40. What does thread safe means?    coderanch.com

Thread safety means that the object that is accessed by multiple threads is always in the valid and the consistent state. More precicely, it breaks down to safety in terms of 3 different concepts: deadlock, starvation and race. The deadlock most commonly occurs when one process requests a resource held by another process that is waiting for the my resource held ...

41. abt Thread Safe    coderanch.com

42. thread safe    coderanch.com

43. thread safe    coderanch.com

44. Thread safe    coderanch.com

45. Using "this" keyword in parent class thread safe?    coderanch.com

I disagree with your disagreement Initialization safety can't be guaranteed, because the field "s" is not marked final. The reference to an instance of the Parent class (let's forego the inheritence hierarchy, as it is not relevant) created in thread A can become visible to thread B, while partially constructed. The field "s" will be initialized to its default value null ...

46. GC - Thread safe    coderanch.com

As per my understanding the thread that run the GC is a JVM/System thread. And the answer to the original question is system dependent i.e. the call to System.gc is actually a native call so it depends on the implementation. But if you are really concerned with whether you should be careful about where you place this call. As per my ...

47. is Logica SMPP Api thread safe?    coderanch.com

i am executing a flow made in Websphere Message Broker, that uses Logica smpp Api to create the connection and send the message, if i run multiple instances of the flow at the same time, messages are not sent, though if i run a single instance of the flow alone, the message is going fine. Kindly can nyone tell me whether ...

48. Thread Safe Classes    coderanch.com

49. Is the following Class Thread Safe ?.    coderanch.com

50. Thread Safe    coderanch.com

51. Create Thread-Safe Classes on Multiprocessor Hardware    coderanch.com

Your goal here is to prevent any data updates from needing to be shared across threads. Once you need to update data in one thread and see it in another - or need to update data in more than one thread you will need one of the synchronization schemes you describe above*. So to the point of excluding data updates: You ...

52. Is this small piece of code thread-safe?    coderanch.com

Hi, this TimingMonitoris a class that can count the execution time of codes: i was trying to use it in this way: TimingMonitor.setLogLevel(Level.INFO) TimingMonitor.start("load file") load file method TimingMonitor.stop("load file") and this is the source of the class public class TimingMonitor { public enum Level { INFO, DEBUG, TRACE; } private static Map map = Collections .synchronizedMap(new HashMap()); private ...

53. implementing a thread safe Publish/subscribe mechanism    coderanch.com

Hi everybody! I am trying to figure out the best possible approach to implement a publish/subscribe mechanism which should work more or less like this: Several Subscribers (1 Thread per Subscriber) subscribe their interest for a given topic T and a given kind of message M by calling initListenerForTopic(T topic) followed by waitNewMessage(T topic). Such threads get blocked until a publisher ...

54. Thread safe code    coderanch.com

55. is repaint() still thread safe?    coderanch.com

I was always under the impression that JComponent.repaint() was always executed on the edt. You can always look at the source code to see what is going on. All the repaint() method does (in JDK6_7) is invoke the RepaintManager.addDirtyRegion(...) method. This adds the components bounds to a Hashmap. Some of the code in this method is synchronized, some is not (there ...

56. Is this thread safe?    java-forums.org

Is this implementation thread safe? How do I make it thread safe? It seems to be working when I run it, but I'm not sure if it's correct. public class Calculate { private int[] list = {0, 0}; public synchronized String getElements() { return list; } public synchronized void setElements(int first, int second) { list[0] = first; list[1] = second; } ...

57. thread-safe increment()    forums.oracle.com

Ever thought about "starting small" and "working your way up" to whatever you are wanting to do? God, why don't you just say "I want a book that will give me both the book and pratical knowledge of a 20 year programmer, overnight, without having to read it." while you're at it.

58. Thread safe doubt    forums.oracle.com

I have to sychronize record sucess as different threads would enter att() and enter record success method and simultaneously update, so i will end up getting a wrong value. Example: First thread come in att().. it takes 20 msec for attach so timetakenforattach = 0 + 20 2nd thread come in att().. it takes 10 msec for attach so timetakenforattach = ...

59. thread safe    forums.oracle.com

60. SwingWorker process(List chunks) but List isn't thread safe is it.    forums.oracle.com

might result in: process("1", "2", "3", "4", "5", "6") Edit: The only source I could find for AccumulativeRunnable (the class used to accumulate the arguments passed to publish) was here: http://fisheye5.cenqua.com/browse/swingworker/src/java/org/jdesktop/swingworker/AccumulativeRunnable.java?r=1.2 which isn't necessarily the implementation in the JDK but it's probably close. That shows that before process is called the accumulating list reference is nulled, meaning that there is no ...

61. LinkedBlockingQueue is thread safe?    forums.oracle.com

Hi all, maybe this is a silly question, but: is LinkedBlockingQueue thread safe? I've read all api docs and tutorials, but it's not explicitly said that this class is thread safe (e.g. for ConcurrentLinkedQueue it's explicitly said). If yes, than all classes in concurrent package are thread safe? Many thanks in advance, Paul

62. thread safe    forums.oracle.com

63. Need all thread-safe classes in java    forums.oracle.com

64. Are SocketFactory implementations thread-safe?    forums.oracle.com

Hi! I'm using a SSLSocketFactory-singleton (SSLSocketFactoryImpl it is I guess) for creating SSLSocket-instances in my application. I just wondered whether or not I need synchronization especially for using the createSocket(...)-methods in my multi-threaded application, since I can't look in the code due to restrictions. Another question is whether the answer applies to other SocketFactory implementations. Greetings

65. Is this class is thread safe?    forums.oracle.com

66. Thread safe Packages    forums.oracle.com

67. String, hashCode and thread-safety    forums.oracle.com

@ejp. Thanks. This is what I was thinking. I was more interested in how the operation of reassigning an instance variable reference is visible to other threads. I'm comparing this to non-atomic operations on ints like incrementing a shared counter. Obviously unsynchronized access to the counter can lead threads to seeing the variable in an "inconsistent" (corrupt?) state. I was wondering ...

68. Thread Safe question    forums.oracle.com

Hi All, i create an object (lets call it "obj1") it creates a file. i also creates on some other class with two threads . i want to use the obj1 in both threads. my question is: is it possible and is it safe to use the same object in two threads?? p.s im not writing to this object (obj1) , ...

69. Is KeyStore thread safe?    forums.oracle.com

70. Thread Safe List    forums.oracle.com

I have an ArrayList that intermittently receives Objects from another thread. I want to make this list thread-safe so I can iterate over it while it is receiving Objects. Vector is a synchronized list but it is fail-fast (if the Vector is structurally modified ... the Iterator will throw a ConcurrentModificationException). That wont work because I dont want the iterator to ...

71. Single Threaded vs Thread Safe    forums.oracle.com

Actually the truth is nearly the opposite. When you say some piece of code is thread safe, you are saying it's written in such a way that it's safe for serveral threads to be running it at the same time. That may involve protecting parts of the code with synchronized, but it's mostly about being careful with common state data.

72. Thread-safe invocation of method?    forums.oracle.com

But I don't see how you be sure that a get operation is safe. HashMap.get(arg) is not an atomic operation. So in theory thread 1 that will return value with key "1" could get thread 2's value because thread 2 with key "2" could overwrite thread 1' key in a hidden implementation of "get".

73. Is this class thread-safe?    forums.oracle.com

marcos_aps wrote: The decision the remove the synchronized block was not a punt. It was a conscious decision based on the explanation I gave, which I think makes sense. You can, of course, show me if my argument was wrong. It looks suspiciously like a variant of the double-check idiom to me; except that you don't use a synchronized block. You ...

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.