answersLogoWhite

0

by using synchronized class

User Avatar

Wiki User

13y ago

What else can I help you with?

Continue Learning about Engineering

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


What are 'drunken' threads?

"Drunken" threads refer to a phenomenon in computer programming, particularly in multi-threaded applications, where threads that are supposed to work in a coordinated manner become disorganized and unpredictable due to improper synchronization. This can occur when threads access shared resources without adequate locking mechanisms, leading to race conditions, deadlocks, or inconsistent data. The term metaphorically suggests that the threads are behaving erratically, much like a person who is intoxicated. Proper thread management and synchronization techniques are essential to avoid "drunken" behavior in threaded applications.


Explain why interrupts are not appropriate for implementing synchronization?

Because they are completely unrelated things? Synchronization can be implemented with semaphores or mutexes.


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.


Thread Is single sequence stream which allows a program to split itself into two or more simultaneously running tasks?

A thread is a lightweight unit of execution within a process that allows for concurrent operations. By enabling a program to divide itself into multiple threads, it can perform tasks in parallel, improving efficiency and responsiveness. Each thread runs independently but shares the same memory space, making communication between threads easier but also requiring careful synchronization to avoid conflicts. Overall, threading enhances a program's ability to utilize system resources effectively.

Related Questions

What is thread synchonisation in os?

The thread synchronization is done for sharing of a common resource, to find the no. of resources in use, its count ,maximum resources in use... To establish this the various synchronization tools used are.. Events Waitable Timer


What is interprocess synchronization?

Processes might need to communicate to each. Interprocess of synchronization is the?æ management of resource among process. It ensures only a single thread (process) access a resource at a particular time.


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


Synchronization in java?

Threads communicate primarily by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors. The tool needed to prevent these errors is synchronization.


How does a monitor prevent multiple threads from concurrently executing inside the monitor?

A monitor is an exclusive locking mechanism available to every managed object. When you are writing instance methods within a class to update and read instance fields, you can utilize the monitor of the current object to synchronize access. A thread can use a monitor to block other threads for the period of time during which it is making a series of updates to an object. That means a monitor can be used in order to block other threads from seeing any single update until all of the updates have been completed.A monitor type presents a set of programmer-defined operations that are provided mutual exclusion within the monitor. The monitor type also contains the declaration of variables whose values define the state of an instance of that type, along with the bodies of procedures or functions that operate on those variables.The monitor construct ensures that only one process at a time can beactive within the monitor. However, the monitor construct, as defined so far, is not sufficiently powerful for modeling some synchronization schemes. For this purpose, we need to defineadditional synchronization mechanisms. These mechanisms are provided by the condition construct. A programmer who needs to write a tailor-made synchronization scheme can define one or more variables of type condition:condition x, y;The only operations that can be invoked on a condition variable are wait ()and signal(). The operation x.waitO ;means that the process invoking this operation is suspended until anotherprocess invokes x . s i g n a l ( ) ;The x. signal () operation resumes exactly one suspended process. If noprocess is suspended, then the signal () operation has no effect; that is, thestate of x is the same as if the operation had never been executed .This is showing how monitor is operating


What are 'drunken' threads?

"Drunken" threads refer to a phenomenon in computer programming, particularly in multi-threaded applications, where threads that are supposed to work in a coordinated manner become disorganized and unpredictable due to improper synchronization. This can occur when threads access shared resources without adequate locking mechanisms, leading to race conditions, deadlocks, or inconsistent data. The term metaphorically suggests that the threads are behaving erratically, much like a person who is intoxicated. Proper thread management and synchronization techniques are essential to avoid "drunken" behavior in threaded applications.


What is difference between synchronization and inter thread communication?

Synchronization is the process by which, access to the objects by threads are controlled and at at time only one thread can access an object. This is done to ensure that there are no dirty read/write operations. If multiple threads access the same data simultaneously, we may end up with inconsistent data. To avoid it, we place such code in synchronized blocks/methods to ensure integrity of data. Inter thread communication is the process by which java threads can communicate with one another. They communicate to one another through some commands like wait, notify, notifyall etc.


What are some strategies for resolving thread contention in a multi-threaded application?

Some strategies for resolving thread contention in a multi-threaded application include using synchronization mechanisms like locks, semaphores, and mutexes to control access to shared resources, implementing thread-safe data structures, reducing the scope of synchronized blocks, and using thread pooling to limit the number of active threads. Additionally, optimizing the design of the application to minimize the need for shared resources can help reduce thread contention.


How busy waiting can be avoided when using the semaphores as a synchronization tool?

Busy waiting can be avoided with semaphores by using blocking calls instead of active polling. When a thread attempts to acquire a semaphore and it is unavailable, the thread can be put to sleep, allowing other threads to execute. This approach minimizes CPU usage and improves overall system efficiency. Additionally, using condition variables in conjunction with semaphores can further enhance synchronization by allowing threads to wait for specific conditions without busy waiting.


What is a monitor according to operating systems?

In the context of operating systems, a monitor is a synchronization construct that enables safe access to shared resources by multiple threads or processes. It encapsulates shared data and provides methods for locking and signaling, ensuring that only one thread can access the resource at a time while others may wait. This helps prevent issues like race conditions and ensures data consistency. Monitors are often used in conjunction with condition variables to manage the execution flow based on certain conditions.


Which provides better quality an interlaced monitor or noninterlaced monitor?

A non interlaced monitor is one where all the scan lines occur sequentially, whereas an interlaced monitor is one where all the odd scan lines occur, followed by all of the even scan lines, in alternating painting of the phospher.


When was Transactional Synchronization Extensions created?

Transactional Synchronization Extensions was created in 2012.