Skip to content

Conversation

@aliviaVi
Copy link
Owner

@aliviaVi aliviaVi commented Dec 4, 2019

No description provided.

Thread t2=new Thread(myThread2);

t1.start();
t1.join();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is wrong: main thread is waiting for the t1 to finish and only then you start t2. We need 2 threads started almost simultaneously. So, you do not have 2 threads competing for the same Counter instance and you will always get correct result (0) because threads run in a sequence, not in parallel.

@Override
public void run() {
for (int i = 0; i <10 ; i++) {
count.increment();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also wrong: one thread (t1) should increment, second, t2, should decrement.

@aliviaVi
Copy link
Owner Author

aliviaVi commented Dec 5, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants