user1427380
user1427380

Reputation: 133

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?

Upvotes: 9

Views: 10793

Answers (7)

Prasad S
Prasad S

Reputation: 35

At any given point in time, only one thread will be executing(live), so Thread.currentThread() returns the details of the currently executing Thread. e.g., Thread_name, priority, method_name.

Upvotes: 0

assylias
assylias

Reputation: 328735

When an instruction in your code is executed, it is executed within a specific thread. This is the thread returned by that method.

Obviously, if a specific method is executed by multiple threads, each execution might return a different value for Thread.currentThread().

You could try this short example to get a better idea of what is going on and in particular the fact that the 2 threads execute in parallel. You should see that t1 will run a few loop then t2 will do the same and back to t1 etc (you might need to increase the number of loops from 5 to a higher number depending on your machine):

public class TestThread {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    System.out.println(Thread.currentThread());
                }
            }
        };

        Thread t1 = new Thread(r, "t1");
        Thread t2 = new Thread(r, "t2");
        t1.start();
        t2.start();
    }
}

Upvotes: 5

Sylvain Defresne
Sylvain Defresne

Reputation: 44553

When writing a single threaded application, you can reason on your program like a series of instruction, each executed after the previous one is finished (this is a rough approximation, but conceptually compilator and processor try to simulate that and work as if this was really what was happening).

When you use multiple thread, each thread has its own series of instruction. A new thread when created is passed a entry point (a method), and will continue executing from them. Each thread is independent from the other thread in their execution (it will simply execute one instruction after the other), though they share memory and thus side-effect from one thread can affect another.

So for some code to be executed, it must be done in the context of one thread (there is one created by the operating system when your application start, that start its execution at the Main method). When the function currentThread is called, it is in one of those context.

The java runtime when creating a thread will store a reference to that Thread object in the thread context, and the currentThread will just lookup there and return the current thread.

Upvotes: 0

Himanshu Mohta
Himanshu Mohta

Reputation: 1043

Multiple threads are not running at the same time and there is thread switching between multiple threads. Processor can execute one task at a time so one thread at a a time executed. So we get the reference of currently running thread.

Upvotes: -1

erickson
erickson

Reputation: 269817

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().

Upvotes: 5

Xeon
Xeon

Reputation: 5989

"The currently executing thread" means that the system scheduler gave some time to this thread to execute its code.

Upvotes: 0

Jean-Bernard Pellerin
Jean-Bernard Pellerin

Reputation: 12670

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.

Upvotes: 7

Related Questions