Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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!

image text in transcribedCounter 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What forms should risk reporting take?

Answered: 1 week ago