how multiple threads invoke singleton object's method and work on them?

前端 未结 4 884
無奈伤痛
無奈伤痛 2021-01-29 20:05

I have multiple threads running which access singleton object and call its method and pass objects in it. In the method I do some calculations only on recieved object. I have he

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-29 20:38

        import java.util.ArrayList;
        import java.util.Collections;
        import java.util.List;
    
        public class MainClass {
            public final static int MAX_THREAD = 20;
            public MainClass() {
                // TODO Auto-generated constructor stub
            }
    
            public static void main(String[] args) {
    
                List singletonList = new MainClass().createSingletonObjects();
                singletonList = Collections.synchronizedList(singletonList); 
    
                int index = 0;
                for (int i = 0; i < MAX_THREAD; i++) {
    
                    Thread thread1 = new Thread(new MyThread(singletonList,index), "Thread"+i);
                    thread1.start();
                    index++;
                if (index == singletonList.size()) {
                index = 0;  
                }
    
                }
    
            }
    
        public synchronized List createSingletonObjects(){
            List listSingleton = new ArrayList();
                listSingleton.add(MySingleton1.getInstance());
                listSingleton.add(MySingleton2.getInstance());
                listSingleton.add(MySingleton3.getInstance());
                listSingleton.add(MySingleton4.getInstance());
                listSingleton.add(MySingleton5.getInstance());
    
                return listSingleton;
    
            }
    
        }
    
    
    public class MySingleton1 extends Singleton{
        private static Singleton mySingleton;
        private MySingleton1() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        public static Singleton getInstance() {
            if (mySingleton == null) {
                mySingleton = new MySingleton1();
                return mySingleton;
            }
            return mySingleton;
        }
    
    
    public class MySingleton2 extends Singleton{
        private static Singleton mySingleton;
        private MySingleton2() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        public static Singleton getInstance() {
            if (mySingleton == null) {
                mySingleton = new MySingleton2();
                return mySingleton;
            }
            return mySingleton;
        }
    
    
    
    
    
    
    
    }
    
    
    public class MySingleton3 extends Singleton{
        private static Singleton mySingleton;
        private MySingleton3() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        public static Singleton getInstance() {
            if (mySingleton == null) {
                mySingleton = new MySingleton3();
                return mySingleton;
            }
            return mySingleton;
        }
    
    
    
    
    
    
    
    }
    
    
    public class MySingleton4 extends Singleton{
        private static Singleton mySingleton;
        private MySingleton4() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        public static Singleton getInstance() {
            if (mySingleton == null) {
                mySingleton = new MySingleton4();
                return mySingleton;
            }
            return mySingleton;
        }
    
    
    
    
    
    
    
    }
    
    
    public class MySingleton5 extends Singleton{
        private static Singleton mySingleton;
        private MySingleton5() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        public static Singleton getInstance() {
            if (mySingleton == null) {
                mySingleton = new MySingleton5();
                return mySingleton;
            }
            return mySingleton;
        }
    
    
    
    
    
    
    
    }
    

提交回复
热议问题