Thread Starts Running Before Calling Thread.Start

thread starts running before calling Thread.start

Because of the trailing () on the target=self.read(), you're running self.read in the wrong thread, the current thread — not the new thread you're creating — and passing the return value of the self.read call as the target argument of Thread. Thread expects to be passed a function to call, so just remove the parentheses and remember to start the thread:

t1=threading.Thread(target=self.read)
t1.start()
print("something")

For targets that need arguments, you can use the args and kwargs arguments to threading.Thread, or you can use a lambda. For example, to run f(a, b, x=c) in a thread, you could use

thread = threading.Thread(target=f, args=(a, b), kwargs={'x': c})

or

thread = threading.Thread(target=lambda: f(a, b, x=c))

though watch out if you pick the lambda - the lambda will look up f, a, b, and c at time of use, not when the lambda is defined, so you may get unexpected results if you reassign any of those variables before the thread is scheduled (which could take arbitrarily long, even if you call start immediately).

Is everything that happened before thread.start visible to the thread that is spawned calling start?

Would the program be guaranteed to print 5 since z happened before thread.start?

If the value of z was set by the thread which called the start() method then yes. Otherwise, no. The newly started thread is guaranteed to see the changes made by the thread that started it, NOT the ones made by other threads.

Also if we talk about it in way, then is it safe to say that thread.start() can never be reordered ever.

The point is - the newly started thread is guaranteed to see the value of k as 8. If the newly started thread does not read k (or any other variables that were set by the parent thread) then compilers are allowed to re-order such operations, but it doesn't matter to the programmer (the programmer gets the guarantee anyway)

And then the new thread happens to find that con to be null?

Assuming that the new thread has a reference to con, con is guaranteed to be initialized before the new thread starts (since the JMM guarantees that any changes made by the parent thread before the call to start() would be visible to the new thread)

To summarize - is a thread (T1) starts another thread(T2), then any changes made by T1 before starting T2 are guaranteed to be visible to T2. As a programmer, this is what matters. Compilers are allowed to perform re-orderings as long as they do not break this guarantee. You could of-course refer to the JLS but I think you already have.

Python 3 | Mutithreading starts before thread.start() is decleared

Don't use () unless you want to run the method immediately. If you want to reference the method itself (like to pass it to Thread), leave off the ().
Try this code:

if __name__=="__main__":
print("Not running")
t1 = threading.Thread(target=timer)
print("clearly running")
t2 = threading.Thread(target=regx)
t1.setName('t1')
t2.setName('t2')

t1.start()
t2.start()

t1.join()
t2.join()

Does std::thread run before I call join()?

It will execute when you instantiate it.

Joining is used so that the current thread will wait until your other thread finishes execution.

Some example code from http://en.cppreference.com/w/cpp/thread/thread/thread

    void f1(int n)
{
for (int i = 0; i < 5; ++i) {
std::cout << "Thread 1 executing\n";
++n;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}

void f2(int& n)
{
for (int i = 0; i < 5; ++i) {
std::cout << "Thread 2 executing\n";
++n;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}

int main()
{
int n = 0;
std::thread t1; // t1 is not a thread
std::thread t2(f1, n + 1); // pass by value
std::thread t3(f2, std::ref(n)); // pass by reference
std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
t2.join();
t4.join();
std::cout << "Final value of n is " << n << '\n';
}

Possible output:
Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 2 executing
Thread 1 executing
Final value of n is 5

is it possible to start a thread by calling run() inside a run()method?

i was referring this article where they have called runnable.run(); inside another run method and it seems to be implying that it starts a new thread.

Looking at the code, I don't see that implication at all.

It's not starting a new thread. It's running the next Runnable from a queue on the current pool thread.

I know that run method should not be called to start a new thread execution...

Not should not, cannot. :-) Calling run just calls run, on the current thread, just like any other method call.

  1. Why thread.start(); is called inside constructor?

To start the thread that was just created with new PoolThread(taskQueue).


  1. How do i enque my task if thread.start(); is called even before calling this.taskQueue.enqueue(task);

You pass it into execute. It gets added to the queue of things to do (taskQueue). One of the PoolThreads that ThreadPool created will pick it up when it's next free.


  1. To understand all these please post a driver class for this example with maxNoOfTasks=10 and noOfThreads=3.and output for the same would be much appreciated.

I don't know what you mean by a driver class, but I think answering the questions is sufficient.


  1. Does Runnable.run() inside run method start a new thread ?

No.


So to understand what this does, say you create a ThreadPool with 5 threads. The ThreadPool constructor creates and starts five PoolThread threads immediately. Those threads constantly check taskQueue to see if there's anything to do and, if so, they do it.

Initially, of course, taskQueue is always empty so the threads are busy-waiting, constantly spinning checking for something in taskQueue. (This isn't really ideal, it burns CPU for no good reason. It would be better to suspend threads when there's nothing to do, but that's starting to get pretty far from the actual question.)

Then, at some point, you call execute and pass in a task. That adds it to the taskQueue. The next time one of the five threads checks for something in taskQueue, it finds it, and runs it on that thread (not a new one).

Thread execution after .start() method

Calling start() on a Thread doesn't necessarily result in the thread running immediately after. It is possible for other things to happen in between your calling start() and the first line of your thread's run() method actually being run. And even once your run() is actually running, it's also possible that other things happen before, during, or after your run() method finishes.

In your question, you said: "assume these just print out a string" – here's an implementation of run() which does that:

public void run() {
System.out.println("my name is: " + getName());
}

So it's possible that t1 starts to run first, but before it actually calls System.out.println, t2 is allowed to execute and runs to completion, then t1 is resumed.

If this kind of behavior won't work for your use case, you'll need to add some kind of concurrency protection to coordinate how and when your threads run.

UPDATE:

To illustrate the unpredictable sequence of thread execution, run this code a few times and observe the output:

public class Example {
public static void main(String[] args) {
for (int k = 0; k < 10; k++) {
new TestThread(k).start();
}
}
}

class TestThread extends Thread {
private final int k;

TestThread(int k) {
this.k = k;
}

@Override
public void run() {
System.out.print(k + " ");
}
}

Here is the output from one of my local runs:

7 0 1 5 4 6 3 2 8 9 

threading: why is thread.start() being block?

You called _start_monitoring, you didn't pass it as a function to run. Change the line to:

threading.Thread(target=self._startMonitoring, daemon=True).start()
# ^ parentheses removed

How run() method is getting invoked when I call Thread.start()?

You can see the native code sources since OpenJDK is an open source software.

For example Github mirror of Thread.c class defines Thread.start0() native method as:

"start0",           "()V",        (void *)&JVM_StartThread},

where JVM_StartThread is defined in jvm.cpp source file.

If you dig deeper into thread creation, which is OS specific, you will arrive to thread_entry() function which calls the Thread.run() method from within the native code:

static void thread_entry(JavaThread* thread, TRAPS) {
HandleMark hm(THREAD);
Handle obj(THREAD, thread->threadObj());
JavaValue result(T_VOID);
JavaCalls::call_virtual(&result,
obj,
SystemDictionary::Thread_klass(),
vmSymbols::run_method_name(),
vmSymbols::void_method_signature(),
THREAD);
}


Related Topics



Leave a reply



Submit