Answer the question
In order to leave comments, you need to log in
Why doesn't the system accept a response without calling this?
Good day.
I took the test yesterday and finally passed it. But there are some questions about this.
Here is the text of the assignment:
In the monitoring system, tracking of each device is implemented in a separate thread.
Each thread must constantly send messages to the server by calling sender.send(message) (sender is a shared object accessible from each TestThread instance)
class TestThread extends Thread {
/*.....*/
String message;
public void run() {
while (true){
/*YOUR CODE will be placed HERE*/
}
}
}
Sending a message is a lengthy process.
It is important that devices send messages in turn, that is, only one thread sends a message at a time.
Add the necessary lines to the code so that all devices transmit the message in the required way.
There is no need to delay the execution of threads.
synchronized (sender) {
sender.send(message);
}
synchronized (sender) {
sender.send(this.message);
}
this.message
and message
. run()
, we implicitly pass a reference to the calling instance there. And Java automatically replaces such variable calls with this. Answer the question
In order to leave comments, you need to log in
I can assume that in the case of threads, the this.message variable means that the thread will work with the data that was at the time of its launch / creation, and not those that actually lie in memory at the moment. If without this, then the data will be taken exactly those that are actually in the variable at the moment, but the thread could sleep and the data should have been sent by others.
This is just a guess, and most likely wrong.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question