Write a method that stores an integer. Each time the method is called, increment
ID: 3700933 • Letter: W
Question
Write a method that stores an integer. Each time the method is called, increment the integer by 1. Create three threads that call this method 1000 times each, storing the returned result in their own set. Print out each thread’s set. Synchronize the method that stores and increments the integer. Execute your threads again. Review thread-specific methods: sleep(), wait(), yield(), suspend(), stop(), resume(), interrupt(), notify(), notifyAll() Write a method that stores an integer. Each time the method is called, increment the integer by 1. Create three threads that call this method 1000 times each, storing the returned result in their own set. Print out each thread’s set. Synchronize the method that stores and increments the integer. Execute your threads again. Review thread-specific methods: sleep(), wait(), yield(), suspend(), stop(), resume(), interrupt(), notify(), notifyAll() Write a method that stores an integer. Each time the method is called, increment the integer by 1. Create three threads that call this method 1000 times each, storing the returned result in their own set. Print out each thread’s set. Synchronize the method that stores and increments the integer. Execute your threads again. Review thread-specific methods: sleep(), wait(), yield(), suspend(), stop(), resume(), interrupt(), notify(), notifyAll()Explanation / Answer
class Test
{
public static void main(String[] args) {
Test test1 = new Test();
test1.doMethod();
}
public int getCount() {
int count = 0;
try {
if ( !new File("d:\myCount.txt").exists())
return 1;
else {
BufferedReader br = new BufferedReader(new FileReader(new File("d:\myCount.txt")));
String s = br.readLine();
count = Integer.parseInt(s);
br.close();
}
} catch(Exception e) {
e.printStackTrace();
}
return count;
}
public void putCount(int count) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("d:\myCount.txt")));
bw.write(Integer.toString(count));
bw.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public void doMethod() {
int count = getCount();
System.out.println("You are running this program " + count + " number of times");
count++;
putCount(count);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.