Reputation: 81
I'm currently getting familiar with threads and multithreading in Java. I'm doing a simple exercise where I have to split the array into equal partitions based on N number of threads that I pass to my function at run time.
However, I'm curious to know if I'm actually executing mergeSort() in each thread, or is it just running as a single thread. I did a for loop, and it was something like Thread.start() followed by a try-catch block, where Thread.join() would be called and then I would print the name using Thread.currentThread(). However, I get the word "main" throughout the entire execution.
Does this mean I'm not really multithreading? My thinking would be that I'd get something like Thread-0..., but I only get that if I do Thread.getName();
Please advise.
for (MedianMultiThread mmt : threadList) {
mmt.start();
try {
mmt.join();
sortedSubArrays.add(mmt.getInternal());
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
EDIT: Realized that I was running currentThread() in the thread, but when I ran it in the run() method, then I got different results. Thanks everyone!
Upvotes: 0
Views: 878
Reputation: 27115
The name thread.currentThread()
method confuses a lot of people. thread.currentThread()
does not return the "current thread" unless you happen to think that "current thread" means, the thread that calls the function.
That's what thread.currentThread()
actually does: It returns a reference to the thread that called it. If it's always returning "main" in your program, then that means that your program only ever calls it from the main() thread.
The name comes from a long time ago when most computers had only one CPU, and it was supposed to mean something to the guy who was writing the thread scheduling code. If you're writing the scheduler for a single CPU machine, then there can never be more than one thread running at any given time. It's natural to call it "the current thread."
The system call that returns the identity of the current thread in an old-school-Unix-style operating system running on a single CPU system could only ever return the identity of the thread that called it.
That turned out to be very useful in user-space programs.
The idea of a "current thread" doesn't mean much on modern multi-processor systems, but a system call that returns the identity of the calling thread still is every bit as useful as ever.
Unfortunately, we're stuck with the old name for it.
P.S., This never makes any sense:
mmt.start();
try {
mmt.join();
...
} catch (...) {
...
}
There's never any reason to have two (or more) threads unless they can both do something useful at the same time. In your code example, one thread starts a second thread, and then the first thread does nothing except wait for the second thread to finish.
You might just as well have done all the work in the first thread.
Upvotes: 1
Reputation: 37347
You have to check in your code, whether function Thread.currentThread()
is called from the actual executing worker threads (and not from the main thread). This means, you need to put your Thread.currentThread()
output inside the run()
methods of the sort-worker threads.
Second advice is that threads are running randomly. I don't think that's the issue, but you need to keep that in mind.
Upvotes: 1
Reputation: 583
Try this:
You need to actually start all threads, then join them all back together,
// start all of them
for (MedianMultiThread mmt : threadList) {
mmt.start();
}
// join all back to main thread
try {
mmt.join();
sortedSubArrays.add(mmt.getInternal());
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
// ... inside your worker thread's run() method
public void run(){
System.out.println("Thread: "+Thread.currentThread());
// do actual work...
}
Upvotes: 1
Reputation: 270
It it is always printing "main", this means that the piece of code which is printing the name of the thread is executed by main thread always, and so that piece is not multithreaded. It would have been better if you had shown the exact code.
Upvotes: 0