✅ Thread and Threading

i have asked this question on the #help-0 an here is the link https://discord.com/channels/143867839282020352/169726586931773440/1269020486952816671 according to conversation with @TeBeCo i have changed the code to be like below by question is now T1.join() will block the main thread till it resolves , and then the T2 will block the main thread and the T1 Thread till it resolves is that correct
c#
static void Main(string[] args)
{
Console.WriteLine("Main Thread Start");

Thread T1 = new Thread(ThreadOneMethod);

T1.Start();

T1.Join();

Thread T2 = new Thread(ThreadTwoMethod);

T2.Start();

T2.Join();

Console.WriteLine("Thread Two Ends");

Console.WriteLine("Thread One Ends");

Console.WriteLine("Main Thread Ends");

}

public static void ThreadOneMethod()
{
Console.WriteLine("Thread One Starts");
}


public static void ThreadTwoMethod()
{
Console.WriteLine("Thread Two Starts");

}

public static void ThreadThreeMethod()
{
Console.WriteLine("Thread Three Starts");
}
c#
static void Main(string[] args)
{
Console.WriteLine("Main Thread Start");

Thread T1 = new Thread(ThreadOneMethod);

T1.Start();

T1.Join();

Thread T2 = new Thread(ThreadTwoMethod);

T2.Start();

T2.Join();

Console.WriteLine("Thread Two Ends");

Console.WriteLine("Thread One Ends");

Console.WriteLine("Main Thread Ends");

}

public static void ThreadOneMethod()
{
Console.WriteLine("Thread One Starts");
}


public static void ThreadTwoMethod()
{
Console.WriteLine("Thread Two Starts");

}

public static void ThreadThreeMethod()
{
Console.WriteLine("Thread Three Starts");
}
14 Replies
MODiX
MODiX5mo ago
steven preadly
can any one give me an opinon about this example to explian the thread.join()method
c#
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main Thread Starts");

Thread threadOne = new Thread(ThreadOneMethod);

threadOne.Start();

//this will stop the current calling thread which is the main thread
//till threadOne Executed and finished

threadOne.Join();

Console.WriteLine("Thread One Ends");

Console.WriteLine("Main Thread Ends");



}
public static void ThreadOneMethod()
{
Console.WriteLine("Thread One Starts");

Thread threadTwo = new Thread(ThreadTwoMethod);

threadTwo.Name = "ThreadOneMethod";

threadTwo.Start();

//this will stop the current calling thread which is the (threadOne) thread
//till (threadTwo) Executed and finished

threadTwo.Join();

//this will be printed after all thread finsished

Console.WriteLine("Thread Two Ends");

}


public static void ThreadTwoMethod()
{
Console.WriteLine("Thread Two Starts");

Thread threadThree = new Thread(ThreadThreeMethod);

threadThree.Start();

//this will stop the current calling thread which is the (threadTwo) thread
//till (threadThree) Executed and finished

threadThree.Join();

Console.WriteLine("Thread Three Ends");
}

public static void ThreadThreeMethod()
{
Console.WriteLine("Thread Three Starts");

}

}
c#
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main Thread Starts");

Thread threadOne = new Thread(ThreadOneMethod);

threadOne.Start();

//this will stop the current calling thread which is the main thread
//till threadOne Executed and finished

threadOne.Join();

Console.WriteLine("Thread One Ends");

Console.WriteLine("Main Thread Ends");



}
public static void ThreadOneMethod()
{
Console.WriteLine("Thread One Starts");

Thread threadTwo = new Thread(ThreadTwoMethod);

threadTwo.Name = "ThreadOneMethod";

threadTwo.Start();

//this will stop the current calling thread which is the (threadOne) thread
//till (threadTwo) Executed and finished

threadTwo.Join();

//this will be printed after all thread finsished

Console.WriteLine("Thread Two Ends");

}


public static void ThreadTwoMethod()
{
Console.WriteLine("Thread Two Starts");

Thread threadThree = new Thread(ThreadThreeMethod);

threadThree.Start();

//this will stop the current calling thread which is the (threadTwo) thread
//till (threadThree) Executed and finished

threadThree.Join();

Console.WriteLine("Thread Three Ends");
}

public static void ThreadThreeMethod()
{
Console.WriteLine("Thread Three Starts");

}

}
Quoted by
<@1190732907942260737> from #help-0 (click here)
React with ❌ to remove this embed.
Kouhai
Kouhai5mo ago
T2 will not block T1, because T2 will only be created after T1 exits Though if the goal is to show case how joining a thread works a simpler example would be better like spawning a sinle child thread that waits for few seconds, and in the main thread you have stopwatch that starts before Join and ends after it, then priting out how many seconds have elapsed .
steven preadly
steven preadlyOP5mo ago
if you dont mind can you give me a written code example the result of the above code is
Main Thread Start
Thread One Starts
Thread Two Starts
Thread Two Ends
Thread One Ends
Main Thread Ends
Main Thread Start
Thread One Starts
Thread Two Starts
Thread Two Ends
Thread One Ends
Main Thread Ends
also i get another example from the docs , i the below case the blocking will be for the thread 1 till thread 2 finish it work , is that correct
c#
internal class Program
{
static Thread thread1, thread2;
static void Main(string[] args)
{
thread1 = new Thread(ThreadProc);

thread1.Name = "Thread1";

thread1.Start();

thread2 = new Thread(ThreadProc);

thread2.Name = "Thread2";

thread2.Start();


}

public static void ThreadProc()
{
Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);

if(Thread.CurrentThread.Name == "Thread1" && thread2.ThreadState != ThreadState.Unstarted)
{
thread2.Join();
}
Thread.Sleep(5000);
Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
Console.WriteLine("Thread1: {0}", thread1.ThreadState);
Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);

}


}
c#
internal class Program
{
static Thread thread1, thread2;
static void Main(string[] args)
{
thread1 = new Thread(ThreadProc);

thread1.Name = "Thread1";

thread1.Start();

thread2 = new Thread(ThreadProc);

thread2.Name = "Thread2";

thread2.Start();


}

public static void ThreadProc()
{
Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);

if(Thread.CurrentThread.Name == "Thread1" && thread2.ThreadState != ThreadState.Unstarted)
{
thread2.Join();
}
Thread.Sleep(5000);
Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
Console.WriteLine("Thread1: {0}", thread1.ThreadState);
Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);

}


}
Kouhai
Kouhai5mo ago
No, main thread is not joining thread 1 Thread 1 however is joining thread2, though it might not even join it because it'll only join thread 2 once thread 2 is started, which might not be the case Imo something simple like that would be good
Thread childThread = new Thread(ChildMethod);
Stopwatch stopwatch = Stopwatch.StartNew();
childThread.Start();
Console.WriteLine("Main thread is joining child thread now");
childThread.Join();
TimeSpan elapsed = stopwatch.Elapsed;
Console.WriteLine($"Running on main thread, elapsed: {elapsed.TotalSeconds:f2} seconds");

void ChildMethod()
{
Console.WriteLine("Running on child thread, sleeping for 5 seconds");
Thread.Sleep(5000);
Console.WriteLine("Done sleeping on child thread");
}
Thread childThread = new Thread(ChildMethod);
Stopwatch stopwatch = Stopwatch.StartNew();
childThread.Start();
Console.WriteLine("Main thread is joining child thread now");
childThread.Join();
TimeSpan elapsed = stopwatch.Elapsed;
Console.WriteLine($"Running on main thread, elapsed: {elapsed.TotalSeconds:f2} seconds");

void ChildMethod()
{
Console.WriteLine("Running on child thread, sleeping for 5 seconds");
Thread.Sleep(5000);
Console.WriteLine("Done sleeping on child thread");
}
steven preadly
steven preadlyOP5mo ago
the child will block the main correct ?
Kouhai
Kouhai5mo ago
Yup, main will be blocked for 5 seconds
steven preadly
steven preadlyOP5mo ago
i am confused in this part main thread are joining The child thread or the child thread joined to the main thread
Jimmacle
Jimmacle5mo ago
i wouldn't call them child threads there is no thread hierarchy
Kouhai
Kouhai5mo ago
True, that's bad naming on my part 😅 Main thread is joining child thread, or a better name would be thread1 Main thread is blocked until the other thread exits
Jimmacle
Jimmacle5mo ago
when you call Join() then whatever thread you called Join() from will block until the thread that you called Join() on exits
steven preadly
steven preadlyOP5mo ago
this is an example that are identical to the on i made in above link but it is smaller i made this to understand the Join behavior sorry for asking o much question and i know that the example above is simpler in this example main thread joined T1 ok and in the threadone method i made another thread on T2 so T1 Will join T2 , T2 will block T1 and T1 Will block main is that correct ?
c#
internal class Program
{
static Thread T1, T2;
static void Main(string[] args)
{
Thread.CurrentThread.Name = "main";

Console.WriteLine("Main Thread Start");

T1 = new Thread(ThreadOne);

T1.Name = "ThreadOne";

T1.Start();

T1.Join();

Console.WriteLine("Main Thread Ends");


}



public static void ThreadOne()
{
Console.WriteLine("Thread One Method Starts");


T2 = new Thread(ThreadTwo);

T2.Name = "ThreadTwo";

T2.Start();

T2.Join();

Console.WriteLine("Thread One Method Ends");

}

public static void ThreadTwo()
{
Console.WriteLine("Thread Two Method Starts");

Console.WriteLine("Thread Two Method Ends");
}

}
c#
internal class Program
{
static Thread T1, T2;
static void Main(string[] args)
{
Thread.CurrentThread.Name = "main";

Console.WriteLine("Main Thread Start");

T1 = new Thread(ThreadOne);

T1.Name = "ThreadOne";

T1.Start();

T1.Join();

Console.WriteLine("Main Thread Ends");


}



public static void ThreadOne()
{
Console.WriteLine("Thread One Method Starts");


T2 = new Thread(ThreadTwo);

T2.Name = "ThreadTwo";

T2.Start();

T2.Join();

Console.WriteLine("Thread One Method Ends");

}

public static void ThreadTwo()
{
Console.WriteLine("Thread Two Method Starts");

Console.WriteLine("Thread Two Method Ends");
}

}
i have read this object
Kouhai
Kouhai5mo ago
Yes, that's correct Main thread started and joined Thread1, Main thread is now blocked until Thread1 exits Thread1 started and joined Thread2, Thread1 is now blocked until Thread2 exits Thread2 started and done executing, which means Thread1 is now free to continue executing which causes it to exit, after that Main Thread is finally free to continue exectuing as well
steven preadly
steven preadlyOP5mo ago
I know that its a little bit of a complicated example but this for me to understand Thank you @Kouhai and @Jimmacle
Want results from more Discord servers?
Add your server