Question
Please elaborate on each of the comments written in the code. I don't really understand the: Runnable r = new Increase(c, 3); Thread t =
Please elaborate on each of the comments written in the code. I don't really understand the:
Runnable r = new Increase(c, 3);
Thread t = new Thread(r);
t.start();
I don't understand why sometimes it prints 0 and other times 3. I really don't understand this comment:
/*
toString() method is called while print the object and but it may print 0,
since main() thread may ends before thread t. Note that sometimes it may print 3 also.
*/
What does this mean "since main() thread may ends before thread t" ?
Thank you for the help!
Counter class:
public class Counter { private int count; public Counter() { count = 0; }
public void increment() { count++; }
public int getCount(){return count;} }
package ch04.threads;
public class Increase implements Runnable { private Counter c; private int amount; public Increase (Counter c, int amount) { this.c = c; this.amount = amount; } public void run() { for (int i = 1; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started