Initializing two threads with the same instance of a runnable

前端 未结 4 1784
眼角桃花
眼角桃花 2020-11-29 05:39

Is it bad programming to initialize two threads with the same instance of a runnable? What difference would it make to initialize with separate instances of a runnable, and

相关标签:
4条回答
  • 2020-11-29 05:48

    Is it bad programming to initialize two threads with the same instance of a runnable?

    Not specifically. However, if the Runnable instance has instance fields, then you'll need to make sure that all access to the fields by the thread is properly synchronized, and this will make the code more complicated.

    What difference would it make to initialize with separate instances of a runnable, and does sharing memory locations at all for the same instance of a runnable have anything to do with performance?

    The memory saved by sharing a Runnable instance between multiple threads is insignificant ... unless the Runnable holds a significant amount of instance data. (And if it does, the chances are that this will make the instance non-shareable.)


    Your H class is an example where sharing instances is safe, but pointless since the memory saving is insignificant. (A Runnable object with no instance fields occupies roughly 8 to 16 bytes, depending on the platform.)

    0 讨论(0)
  • 2020-11-29 06:04

    To make understand easily(based on the comment of Stephen), added the below program block about the impact of accessing the instance variable from a non-synchronized block with the same instance of Runnable displays the unexpected results.

    public class SynchronizedInstanceMethod implements Runnable{
    
    private int counter;
    
    public SynchronizedInstanceMethod(int counterValue){
        this.counter = counterValue;
    }
    
    private synchronized void displayMessage(){
        System.out.println(" Display Message ");
    }
    
    private void modifyCounter(){
        this.counter++;
        System.out.println("Value -- "+ this.counter);
    }
    
    @Override
    public void run() {
        this.displayMessage();
        this.modifyCounter();
    }
    
    public static void main(String[] args) {
        SynchronizedInstanceMethod instance = new SynchronizedInstanceMethod(5);
        new Thread(instance).start();
        new Thread(instance).start();
    }
    }
    
    0 讨论(0)
  • 2020-11-29 06:11

    Since H doesn't have any instance state, using multiple instances won't matter. You need to take care when the Runnable instances start storing state.

    public class Main implements Runnable {
        volatile int i;
            public void run() {
            for (i = 0; i < 100; i++) {
                System.out.println(i);
            }
        }
    
        public static void main(String[] args) {
            Main a = new Main();
            Thread t1 = new Thread(a);
            Thread t2 = new Thread(a);
            t1.start();
            t2.start();
        }        
    }
    

    What gets printed? When you do need to share state between threads, it's a good idea to use the classes in java.util.concurrent. They were written primarily by an expert in multithreading (Doug Lea, author of Concurrent Programming in Java) and tested by many people. Save yourself some heartache. :)

    0 讨论(0)
  • 2020-11-29 06:12

    It's absolutely fine to do it so long as the code you're running is designed to support that. Not only will it save some memory by having a single instance instead of multiple instances, but if those threads are trying to communicate via shared data, then it may be absolutely required!

    Admittedly communicating via shared state is where threading often gets tricky, so this needs to be done carefully, but from the point of view of the threading system itself, there's absolutely no problem in having two threads call the run method of a single Runnable instance.

    0 讨论(0)
提交回复
热议问题