user_vs
user_vs

Reputation: 1043

Will Multiple Threads cause Concurrency issue with static methods?

I have a Thread scenerio , in which 3 classes are MainThread.java,NormalWorkerClass1.java,NormalWorkerClass2.java

1 class:

class MainThread implements Runnable

{
private Thread thread = null;
//private variables
..
..
 //default Constructor

 public MainThread(){}

public MainThread(int val){
 this.val=val;
}

    public void start() {
            thread = new Thread(this,"rootthread");
            thread.start();
        }

@Override
    public void run() {

    NormalWorkerClass1 instance1=NormalWorkerClass1.getInstance();  // Normal class
    NormalWorkerClass2 instance2=NormalWorkerClass2.getInstance(); // for other working
    try
        {
            while(true)
            {
                boolean retval=proccessSomething();
                if(retval)
                    {
                      instance1.doMainProcess(arg..);
                    }
                else
                {
                     instance2.doMainProcess(arg..);
                }
            }
        }
    }

2 class:

class NormalWorkerClass1
    {
      private ...
      private variables
        public static NormalWorkerClass1 getInstance() {
            return new NormalWorkerClass1();
            }

           public void doMainProcess(arg..)
        {

            Files processing()  
            // same common methods in NormalWorkerClass2
            UtilityAccess ad=UtilityAccess.getInstance();
            ad.Web Service part()
            ad.dB part()
            ad.Mail sending()
        }
    }

3 class:

 class NormalWorkerClass2
    {
       private ...
      private variables
        public static NormalWorkerClass2 getInstance() {
            return new NormalWorkerClass2();
            }

           public void doMainProcess(arg..)
        {
            Files processing()
            // same common methods in NormalWorkerClass1
             UtilityAccess ad=UtilityAccess.getInstance();
            ad.Web Service part()
            ad.dB part()
            ad.Mail sending()
        }
    }

These are 3 classes. My doubts are:

1 )In a multi threading Environment , i.e. if both class 2 and class 3 accessed at same time , whether 2 and 3 class cause any concurrency issue, because both are using some common methods and classes?

There is no Synchronisation part in this. The web service part consists of another thread part.

2) What will happen when multiple thread access this,

NormalWorkerClass1 instance1=NormalWorkerClass1.getInstance(); // Normal class NormalWorkerClass2 instance2=NormalWorkerClass2.getInstance(); // for other working

because its getInstance() method is a static method , multiple threads will share NormalWorkerClass1 and NormalWorkerClass2 class object values ?

5)Both classes NormalWorkerClass1 and NormalWorkerClass2 calls same common methods.. for e.g.. web service part.. if a thread1 enters into web service part and takes some time to complete ..on that particular moment another thread2 came to use web service part..this might cause any problem in total execution . same case with mail part also..will cause any issue in object clashing. I know each thread has its own stack for execution and have copies of variables

4) Can this Code cause any performance bottleneck? If yes ,How can I improve this code for multi threading and performance improving environment. ?

as i am new to this threading concurrency part..

Upvotes: 6

Views: 3730

Answers (2)

pkalinow
pkalinow

Reputation: 1741

Nathan's Hughes answer is correct. I would add that there may be a concurrency problem if your run() method touches any instance variables of the MainThread class.

And one more thing - maybe obvious, maybe not: concurrency is about threads, not classes. Both NormalWorkerClass1 and NormalWorkerClass2 cannot conflict with each other when they are called from the same thread.

Upvotes: 0

Nathan Hughes
Nathan Hughes

Reputation: 96444

Where concurrency causes problems is when multiple threads access shared state, your example doesn't have any shared state, it just shows static methods returning new instances of things. If you add static class variables that are accessed concurrently then you will have to worry about thread-safety issues with threads overwriting each others' work or with changes not being visible to other threads.

Calling methods doesn't in itself introduce concurrency problems, accessing and changing the contents of instance and class variables is what causes problems.

Upvotes: 6

Related Questions