answersLogoWhite

0

What is thread safe object?

Updated: 12/21/2022
User Avatar

Wiki User

13y ago

Best Answer

A thread safe object is one that would be safe even if multiple thread instances are accessing it. For example if a single object is being updated by a thread and the same object is being read by another thread - we will end up with an inconsistency where the reading object is reading incorrect data because by the time it finishes reading, another thread would've updated it. So we use the keyword synchronized with methods to ensure that such a situation does not happen.

An object that wont let two threads access it simultaneously is called a thread safe object

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is thread safe object?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

If Runnable interface is better than Thread class than why you are using Thread class What is the need for Thread class?

Though implementing Runnable interface is better approach than inheriting from Thread class but there are certain methods available in Thread class like join,sleep or yield which does not available in Runnable interface and programmer can not use them unless he has object of Thread class. This is why we should have Thread class Object. Rupesh Raghani


What is the name of the method used to schedule a thread for execution?

"start" You've created a Thread object and it knows its target. Now it's time to get the whole thread thing running. It's pretty straight forward: t.start(); Prior to calling start() on a Thread instance, the thread (when we use lowercase t, we're referring to the thread of execution rather than the Thread class) is said to be in the new state as we said. The new state means you have a Thread object but you don't yet have a true thread. So what happens after you call start()? • A new thread of execution starts (with a new call stack). • The thread moves from the new state to the runnable state. • When the thread gets a chance to execute, its target run() method will run.


What is difference between thread based and use based strategies for integretion testing in object - oriented methodology?

difference b/w thread based and use based testing


Difference between yield method and sleep in java?

Thread.sleep(long milliseconds): The thread's sleep method sends the current thread into Non-Runnable state for the specified amount of time. But, this doesn't cause the thread to loose ownership of the acquired monitors. So, if the current thread is into a synchronized block/method, then no other thread will be able to enter that block/method.The Sleep method throws 'InterruptedException' if another thread interrupts it.There is another variant of the 'sleep()' method where it accepts two arguments - one, long milliseconds, and second, int nanoseconds. Simply put, this method causes the current thread to sleep for the specified number of milliseconds plus the specified number of nanoseconds. The second argument 'int nanoseconds' can acquire a value of the range 0-999999.wait() method: The wait() method also sends the current thread into Non-Runnable state like the sleep() method. But, the difference between the two is that in case of 'wait()' the locks are released before going into Non-Runnable state, so that any other thread that is waiting on the object can use it (Unlike the Sleep method - This is the big difference) Another apparent difference is that 'wait()' is an instance method, while sleep() is a static method. The method 'wait()' should be called for an object only when the current thread has already acquired lock for that object. This causes the current thread to wait either another thread invokes the 'notify()' method or the 'notifyAll()' method for this object, or a specified amount of time has elapsed and after that the thread starts participating in thread scheduling process to acquire the monitor of the object to proceed further.There are three variants of this method in the 'Object' class:-public final void wait(long timeout)public final void wait(long timeout, int nanoseconds)public final void wait()All the three methods throw InterruptedException & IllegalMonitorStateException. The first two may also throw IllegalArgumentException.The wait() method causes the current thread to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. After the execution of this method invocation, the thread becomes disabled for any scheduling purposes and lies dormant until one of the following things happen:-* Any other thread invokes 'notify()' method this object and the thread under consideration is arbitrarily chosen as the thread to be awakened. * Any other thread invokes 'notifyAll()' for this object. * Any other thread interrupts the thread under consideration. * The specified amount of time has elapsed (in case first two variants of wait() are used) After any of the four above mentioned events happens, the thread is removed from the wait set for this object and re-enabled for thread scheduling. It'll compete for the rights to synchronize on the object in an usual manner and it'll keep doing this until it acquires control on the object and gains all the synchronization claims on the object, which it had acquired at the time of 'wait()' invocation. This means that after the return from wait() method, the synchronization state of object and of the thread will be exactly the same as it was before the wait() invocation.


What is dfference between synchronization and asynchronization in java with example?

synchornisation is the process where more than one thread can be accessed by accesssing the shared object of the class.By syncornisation we can have the communication between threads.if one thread works then another thread automatically stops

Related questions

Is Servlet instance Thread safe If not how can you make thread safe?

No. The Servlet is not thread-safe by default. You can make it thread safe by implementing the SingleThreadedModel interface


Is jsp thread safe by default?

No. JSPs are not thread safe by default


What is object locking in java?

Object Locking is the concept by which a thread that is running obtains a lock on an object that it wants to access. This is done to ensure that no other thread can modify the object that the current thread wants. Multiple threads modifying the same object can result in deadlocks, race conditions and inconsistent data


Is thread safe for parrots?

If it is large enough.


What object pulls thread through fabric?

Needle.


If Runnable interface is better than Thread class than why you are using Thread class What is the need for Thread class?

Though implementing Runnable interface is better approach than inheriting from Thread class but there are certain methods available in Thread class like join,sleep or yield which does not available in Runnable interface and programmer can not use them unless he has object of Thread class. This is why we should have Thread class Object. Rupesh Raghani


Is struts thread safe?

Struts 1 Actions are singletons and must be thread-safe since there will only be one instance of a class to handle all requests for that Action. The singleton strategy places restrictions on what can be done with Struts 1 Actions and requires extra care to develop. Action resources must be thread-safe or synchronized. Struts 2 Action objects are instantiated for each request, so there are no thread-safety issues. (In practice, servlet containers generate many throw-away objects per request, and one more object does not impose a performance penalty or impact garbage collection.)


A fungal thread that runs horizontally across the surface of an object is a what?

rhizoid


What is the name of the method used to schedule a thread for execution?

"start" You've created a Thread object and it knows its target. Now it's time to get the whole thread thing running. It's pretty straight forward: t.start(); Prior to calling start() on a Thread instance, the thread (when we use lowercase t, we're referring to the thread of execution rather than the Thread class) is said to be in the new state as we said. The new state means you have a Thread object but you don't yet have a true thread. So what happens after you call start()? • A new thread of execution starts (with a new call stack). • The thread moves from the new state to the runnable state. • When the thread gets a chance to execute, its target run() method will run.


A fungal thread that runs horizontally across the surface of an object?

stolon


Why did the narrator feel unsafe in the safe house story?

[object Object]


Is 10 days after menstruation safe?

When you count ten days from the day your menstruation started is safe. Linda Adams