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
if(thread.isalive())
{ // Terminate it such that no CPu memory and resources should be used
}
I want to terminate the threads so that no computers resources and memory would be used.Please Guide me in this
In Java threads will terminate itself after the code in it finished. You don't need to do this manually. If you want to terminate a running thread just call interrupt and the thread will terminate.
You wouldn't find a live thread after you started it unless:
A) It is still running
B) It is taking a while for the teardown procedure
Therefore, don't. If a thread still runs, it runs for a good reason. If you need task cancellation, there are better ways, such as interruption, boolean check within the run() method.
The Thread.stop() method will terminate the thread but it is deprecated:
http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html
To terminate a thread, it have to reach the return statement in his run method. You can handle the return by checking a variable and set it as you need.
Related
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.
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 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
I'm struggling with threads and thread blocking.
I have an android activity that has 4 threads that will run in it. These threads need to run without getting the ANR error and they also have to run one after the other so the first thread runs then only when it completes does the second thread start to run etc. All the threads are running different code and the action has to loop round a set amount of times. I have done an example of my problem below. Don't get hung up on why they have to be done in threads they just do because of how the code runs, also its the whole issue of how to do the thread locking that I'm interested in. The questions take a lot of computation to come up with and so does the checking of the answer.
Also need to know how to stop the thread running completely if the back or home button is pressed while the thread is running.
i.e.
Number of questions is how many times the process loops.
first thread
count down timer 3 2 1 before first question is shown. Using count down timer for this so can wait for the onFinnish() before it goes onto next thread
second thread in an implement runnable
display the question to player
third thread in an implement runnable
player answers the question
forth thread this will be a count down timer again
show the result i.e. wether the player answered correctly.
Many help with this in advance.
Cheers
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
How can I know when the thread was stoped and the processor moves to another thread..
Transition between threads harms my calculations, there is some way to know if the thread left and returned to my function?
You can not know when your thread was stopped and the system rescheduled another thread.
But - you can minimize the number of times this event happens by setting the affinity of your thread to one of the processors, and the affinities of the other threads to the other processors.
If you are using Linux, you can use taskset for each thread in the system (get the list by "ps -e") to set the affinities of the other threads to other processors.
This will decrease the load on the processor and will cause it to context-switch less times.
The simple answer is - you can't. Even if you could detect thread context switches it happens far too often to usefully logged.
A better question would be to look at why you need to know. If there is a problem there post it as a question and we can solve the real issue.
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.