Only one thread is required for synchronization


Chapter 135

If I have a class that extends Thread with static methods (this is very simplified):

public class MyThread extends Thread {

    private static long SLEEP_INT = 30000;
    private static Map<Integer, String> myData;

    //every 30 seconds, update map of data
    public void run() {
        while(isActive) {
            try {
                populateDataFromDB();
                Thread.sleep( SLEEP_INT );
            }
            catch( Exception e ) {
                //do nothing
            }
        }
    }

    //static method to update map of data
    public static void populateDataFromDB() {
        //do stuff here, setting values in myData
    }
}

Then elsewhere in my app:

MyThread.populateDataFromDB();

If I know there is only one instance of the MyThread class in the application, do I still need to write synchronization code inside populateDataFromDB to ensure thread safety?

grab

You do need synchronization as you will have multiple threads accessing the data held in the MyThread.myData. You've shown us a thread that will periodically read from your database and populate your map. You only do this if you have something that will leverage this data.

You don't want the thread using the map to see a half-filled map or a map that contains inconsistent state. To be on the safe side, you may want to use synchronization to block the thread reading myDatawhen the MyThreadthread updates it every 30 seconds .

In other words, just because you only have one instance of a given class, doesn't necessarily mean you don't need synchronization. You need synchronization because you have multiple threads (each thread potentially running different code) accessing the same data. You may be able to allow simultaneous access to the data by all readers of the data, but make sure to have exclusive access during operations that write to the data structure.

Related


Thread synchronization (lock) only released to the last thread

Andrew Hanlon What is the correct way to only grant access to the mutex/locked region to the "last entry" thread, while ensuring that intermediate threads do not acquire the lock? Example sequence: A acquires lock B waits C waits B fails to acquire lock* A rel

C# read-only list thread synchronization

Justin Mathew What I'm doing here is browsing a read-only list in multiple threads via for-each and index methods. The result looks thread safe, but I don't believe it. Can someone tell if the code below (reading from a read-only list) is thread safe? If yes,

Required for only one of the two buttons

easymoden00b I have a form whose text field is required for one (1) of my two (2) buttons. The first (1st) button in the text field applies the code to the products in the cart section of my store. The second (second) removes all codes from all products in the

The required html is for only one form

Christman I have a html page with two forms, one for registration and one for login, i have made all fields required, but i want to do this, only registration fields are required, when you register When only the login field is required for you to log in. Here

thread synchronization

Hello and goodbye The following code creates a new cost Threadand waits for the thread to finish before the main thread becomes active again. I don't quite understand how it works. Why not mythread.wait();call now? Why not use it Thread.join()? public static v

Thread synchronization (lock) only released to the last thread

Andrew Hanlon What is the correct way to only grant access to the mutex/locked area to the "last entry" thread while ensuring that intermediate threads do not acquire the lock? Example sequence: A acquires lock B waits C waits B fails to acquire lock* A releas

Thread safety when writing to only one thread

Rich Lonsdale I know if two threads are writing to the same location, I need to make sure they are writing in a safe way and not causing any problems, but if only one thread reads and completes all writes and the other thread just What to do with reading. In m

Thread synchronization?

screwdriver I have a list of commands that are processed simultaneously on multiple threads. The list is static, each thread generates its output and doesn't interfere with other threads, so everything works fine so far. Some commands require complex computati

A synchronization object locks one thread and releases another

Andreas Reiff I have a situation where I want to acquire (lock) a resource in a function call, but in a callback (different thread) I'm told the process ends. (The resource is external: basically, when I start, a certain bus gets busy and becomes free again in

Required for only one of the two buttons

easymoden00b I have a form whose text field is required for one (1) of my two (2) buttons. The first (1st) button in the text field applies the code to the products in the cart section of my store. The second (second) removes all codes from all products in the

A synchronization object locks one thread and releases another

Andreas Reiff I have a situation where I want to acquire (lock) a resource in a function call, but in a callback (different thread) I am told that the process has ended. (The resource is external: basically, when I start, a certain bus gets busy and is free ag

Thread synchronization (lock) only released to the last thread

Andrew Hanlon What is the correct way to only grant access to the mutex/locked area to the "last entry" thread while ensuring that intermediate threads do not acquire the lock? Example sequence: A acquires lock B waits C waits B fails to acquire lock* A releas

thread synchronization

Hello and goodbye The following code creates a new cost Threadand waits for the thread to finish before the main thread becomes active again. I don't quite understand how it works. Why not mythread.wait();call now? Why not use it Thread.join()? public static v

Thread synchronization?

screwdriver I have a list of commands that are processed simultaneously on multiple threads. The list is static, each thread generates its output and doesn't interfere with other threads, so everything works fine so far. Some commands require complex computati

A synchronization object locks one thread and releases another

Andreas Reiff I have a situation where I want to acquire (lock) a resource in a function call, but in a callback (different thread) I am told that the process has ended. (The resource is external: basically, when I start, a certain bus gets busy and is free ag