Whether the thread runs immediately?

xiaoxiao2021-03-06  20

Do you think the following programs output 10 "no"?

Public class mythread implements runnable {string mystring = "yes";

Public void run () {this.mystring = "no";

Public static void main (string [] args) {mythread t = new mythread (); new thread (t) .start ();

For (int i = 0; i <10; i ) system.out.print (t.mystring);}}

First, you can purely, the result is unpredictable, that is, how many "no" or if there is "Yes" or if it is also included.

When this line "New Thread (T) .Start ()" is executed, if the thread at this time is executed immediately and there is no delay, then the result is definitely 10 "no", but the start method is not What thread's RUN method is called to force threads, but the start method simply issues, informs the operating system and other threads in execution, say "I am an executable at this time", and in thread When the queue is performed to this thread, this thread is only executed. So on these cumbersome steps, the main program thread may have already reached the For loop, but the thread of the Start method that may be executed Not implemented yet, this output may be a few "yes" plus a few "no", the number of the two is not known (but there is a certain thing, "yes" and "no" add up The total number is definitely 10), because anyone can't predict what the notified thread is performed.

转载请注明原文地址:https://www.9cbs.com/read-43159.html

New Post(0)