Will multiple threads cause a Concurrency problem with static methods?

I have Thread scenerio in which 3 classes 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..); } } } } 

2nd 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() } } 

Grade 3:

  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:

1) In an environment with multiple threads, that is, if both classes 2 and class 3 are available at the same time, regardless of whether 2 and 3 cause any concurrency problem, because both use some common methods and classes?

This is not part of the synchronization. Part of the web service consists of another part of the stream.

2) What happens if multiple threads access this,

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

because its getInstance () method is a static method, will multiple threads share the values ​​of the NormalWorkerClass1 and NormalWorkerClass2 classes?

5) Both classes NormalWorkerClass1 and NormalWorkerClass2 call the same general methods .. for example, part of the web service .. if stream1 is part of the web service and takes some time to complete. At that particular moment, another thread2 came to use the network of the service part .. this can cause any problem when fully executed. the same case with the mail part also .. will lead to any problem in the conflict of the object. I know that each thread has its own stack to execute and has copies of variables

4) Can this code cause a performance bottleneck? If so, how can I improve this code for a multi-threaded and performance-improving environment.

since I am new to this topic concurrency part ..

+4
source share
2 answers

Where concurrency causes problems, when multiple threads access a common state, your example does not have a common state, it just shows static methods returning new instances of things. If you add static class variables that are accessed at the same time, you will have to worry about thread safety issues when threads overwrite each other's work or changes are not visible to other threads.

The calling methods alone do not present concurrency problems, accessing and changing the contents of instance and class variables, which causes problems.

+6
source

Nathan Hughes answer is correct. I would add that a concurrency problem may occur if your run() method concerns any instance variables of the MainThread class.

And one more thing - maybe, obviously, 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.

0
source

All Articles