CastleLab / COMP3021-2020Fall-PA3-Student-Version

HKUST - COMP3021 - 2020Fall - Programming Assignment 3 - Student Version
https://course.cse.ust.hk/comp3021/
Apache License 2.0
1 stars 0 forks source link

Pause Piece Action #32

Closed zcmiyano closed 3 years ago

zcmiyano commented 3 years ago

In the hint of Pause Piece Action, it is said that

  • elated methods: * - {@link Piece#pause()} * - {@link Thread#interrupt()} *

Do we need to use thread#interrupt() here? It is also used in the Piece terminate action. Can an interrupted thread be resumed?

Troublor commented 3 years ago

interrupt() will cause the thread to throw an InterruptedException. If you didn't catch such exception, the thread will exit and it will not work anymore. If you catch it, the thread will just continue running.

The reason why you are suggested to call Thread.interrupt() is that the thread may be waiting on the paramQueue, when you call pause(). So It will not go in to paused state since it is blocked. interrupt() is used to interrupt the thread and make it stop blocking and pause itself.

You can choose to not use interrupt() if you can implement the functionality in your own way.

zcmiyano commented 3 years ago

Thanks for your reply, may I ask why the thread may be waiting on the paramQueue (what's it?) when I call pause() (in the piece?)?

Troublor commented 3 years ago

paramQueue is one way to let main thread (which calls getCandidateMove) notify piece thread that it's time to propose a move. The parameters are delievered to piece thread through the queue.

You can also choose not to use this queue (and also interrupt()) as long as you implement the functionality correctly.