Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current Thread Method java

So I am trying to work with threads for a game I am making. I am very new to the subject so I may not understand correctly. My question is how does the currentThread() method work in the Thread class of the java API. The API says "Returns a reference to the currently executing thread object.", but as far as I understand multiple threads run at the same time. How is it possible to return only one executing thread?

like image 746
user1427380 Avatar asked May 31 '12 22:05

user1427380


2 Answers

The code that calls currentThread will be executing in one of the threads, not in all of them, so it can get that thread specifically.

like image 111
Jean-Bernard Pellerin Avatar answered Sep 24 '22 23:09

Jean-Bernard Pellerin


Suppose you have list of instructions printed on a piece of paper. A person reads the instructions and performs them. The instructions are a program. The person is a thread. You could make many copies of the paper and pass them out to many people. If the instructions say something like, "slap yourself," yourself refers to whomever is reading that instruction from that paper. Likewise, Thread.currentThread() refers to the thread that is executing that call to currentThread().

like image 36
erickson Avatar answered Sep 22 '22 23:09

erickson