print stack with changing values with Thread [closed] - java

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.

Related

How to create a thread without ThreadGroup? [closed]

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

Android background thread in 2021 [closed]

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
Can you explain short guide about background thread in 2021? I mean what is last recommendation to use, what was deprecated.
When I tried to found the information about it, I have confused. One place told about Thread, another Executor, the next about AsyncTask and so on. Every contains note or comment about obsolescence and not recommended by Google. What is true?
It really depends on what you are trying to do. Generally, the guide to background processing which was already linked by Dmitry in your comments is a good place to start.
Regarding the things you mentioned:
A Thread is useful if you have long-running tasks which do not happen all the time. Then you can create a thread whenever needed and it will be destroyed as soon as it's done executing.
By Executor I guess you are referring to the use of an ExecutorService. This is a class to manage a pool of threads. That is, you can give a job to the ExecutorService and it will assign it to any of its threads for execution. This is more efficient than creating a thread yourself every time, because the threads are not immediately destroyed after their work is complete. However, they will also stay in the memory because of that. Thus, an ExecutorService is suitable if you have short background jobs which occur often.
As for the AsyncTask, this API is indeed deprecated by now.

java multithreading method that can acess atmost 3 threads concurrently [closed]

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

One thread for each mob in a 2D game: Will it be system-resource consuming? [closed]

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
I'm developing a java 2D game, with a Board class and Mob(s) class. The board class has a thread that calls 'repaint()' every a specified delay, while the mobs class constructs a new thread for each of the mob spawned, which means when it does something, it ticks its time on its own. Simply said, I seperate the thread to enable frame rate setting. So i just need to only set how long does the Board thread sleep, like faster sleep means more frame rate (I use threads for this because a site says threaded timing can be made real precise). But then it means in a crowded in-game situation there would be a lot of thread running from every active Mob there.
My question: if i have this lots of thread running at the same time, wouldn't it be consuming a lot of system resources? And if yes, what is the best way round so I don't need to use threads? (just to note 2D game "crowded" situation can be "really crowded").
I think your question could resume to one of these :
Why is creating a Thread said to be expensive?
How expensive is creating of a new thread in Java? When should we consider using of a thread pool?
Is it expensive to create the Thread object or to actually start the thread?
etc...
Plenty of resource to find info on the subject.
what is the best way round so I don't need to use threads
This question is not appropriate for SO (too broad)

android java threads running one after another but wait for each to complete before running next [closed]

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

Categories