Create variable on class level is not recommended if you are working on multithreading environment. To test how bad is using shared resource in multithreading environment please use below java program to test:

Different threads calling same method at same time java test class :

package com.javahonk.multipleextends;

public class ThreadTestString {

	String value2 ="ABC";

	public static void main(String[] args) {
		final ThreadTestString testString = new ThreadTestString();
		Runnable t1 = new Runnable() {
	        public void run() {
	        	testString.m1("123","A");
	        }
	    };

	    Runnable t2 = new Runnable() {
	        public void run() {
	        	testString.m1("DEF","B");

	        }
	    };

	    new Thread(t1).start();
	    new Thread(t2).start();

	}

	void m1(String value , String collar){

		value2=value;		
		for (int i = 0; i < 25000; i++) {
			System.out.println("Collar: "+collar+" value: "+value2);
		}
	}

}

Leave a Reply

Your email address will not be published. Required fields are marked *