Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to run simultaneously two different methods in two different classes
Is run method is the only way?
If it is Why??
Your code snippet does not show how you actually start these threads, I am assuming something like new Thread2().start(). What this does is create a new thread and the new thread proceeds (in the background) to execute its run method (and then terminates when that method returns).
Every thread runs code that is contained with a run() method. That is just how threads work. There has to be some convention to let the JVM know where the code for the thread is.
From within that method, you can call any other method on any other object that you have access to.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This question was asked in an interview for the Senior Developer Role.
As I don't have any exposure to Java multi-threading and concurrency. So, I could not answer this.
Every thread we create is part of a ThreadGroup. Then how do we create
a thread without ThreadGroup?
It is not possible to create a thread without ThreadGroup.
For more details look here: https://www.eg.bucknell.edu/~mead/Java-tutorial/essential/threads/group.html
"If you create a new Thread without specifying its group in the
constructor, the runtime system automatically places the new thread in
the same group as the thread that created it (known as the current
thread group and the current thread, respectively). So, if you leave
the thread group unspecified when you create your thread, what group
contains your thread?
When a Java application first starts up, the Java runtime system
creates a ThreadGroup named main. Unless specified otherwise, all new
threads that you create become members of the main thread group."
And here: http://www.java2s.com/example/java-book/thread-group.html
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Why we we use Runnable interface even it has no connection with start() method? Why we can't just write run() method and start?
Why we need to implement run() method,instead of using it directly and start process using start() method?
If you just called the run method directly, it would run on the thread you used to call it. By implementing Runnable and passing your instance into new Thread, you're setting it up so that run will be called on the new thread.
I recommend working your way through the Java Concurrency tutorial, which will go into creating and running threads in detail.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Hi today in interview they asked a multi threading question that create a procedure in which there is a method getDBConnect() so that atmost 3 threads can access it concurrently. if 4th thraed try to access getDBConnect() method then 4th thread will go on wait state if anyone of 3 thread release the method getDBConnect() then 4th thread will access the getDBConnect() method using simple thread or executor thread.
Please help me to understand how can i make program of multi threading so that above criteria should be satisfied.
I'm not going to write code for you. But I can hint what this question is about. There is very interesting primitive of multithreading synchronization called Semaphore. JDK contains detailed description and sample of use https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Last week I went for an interview, and during the interview one of the interviewer asked this question with me,
Q. What precaution do you take will writing a run() method?
and my answer was deadlock, livelock, synchronization, starvation and few of the overhead, but
he was not satisfied with my answer. He told no there are some other thing when we write run() method you should take care of...
I wrote run() like this during interview..
class DrawCircle implements Runnable {
public void run(){
//some code here
}
}
can any body tell me what is proper way of writing run() in any Runnable thread?
Nothing obvious but these:
Make sure your run method actually returns after finishing the task or it timeout after some defined threshold. If run() is stuck somewhere then thread will never finish. And if for some reason all your threads are getting stuck, and you have large number of threads, then it may crash your system.
If you are extending the Thread class, then make sure that you override the correct method. To be safe, adding #override on top of the method is good.
If you are implementing an anonymous thread then make sure you assign it to a Thread instance. If your run method goes into a bad state/infinte loop. You may be able to interrupt the thread using the instance.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My understanding is that it creates another thread and runs compute() in another thread.
then join fetches the result once recursively got it.
I would like to know whether fork() calls compute() or not. Thanks in advance.
The fork method will not call compute. It will push the forked task to a work queue in which the running thread determines if it should eventually call compute itself or notify other threads to steal that task and invoke compute.