java-threads

Returning value from thread Java/ Android

限于喜欢 提交于 2019-12-19 11:36:18
问题 I have the following thread in my android class. How can I get the value of err from the thread??? public int method(){ new Thread(new Runnable() { int err; @Override public void run() { err = device.verify(30, 5, coderChoice, detectModeChoice, 0, listSearch, callbackCmd, MTFPSD.this, matchingScore); updateView("Finger Captured Successfully", err); } }).start(); return err; } I want the value to be the return value of the method but for the life of me I can't get the value... 回答1: You have

How java threads are scheduled?

删除回忆录丶 提交于 2019-12-19 03:42:08
问题 I have recently started multi-threaded programming with Java in case of Linux threads , i know that the kernel schedules them(as they are the unit entities that are scheduled)but java programs are run on JVM which in my system (RHEL 6.1) is implemented as a program that is run as a user space instance .So, without the kernel being aware of the java threads, how come preemptive multitasking is done in JVM? it will be helpful if the whole mechanism of JVM and kernel interaction in doing this

DestroyJavaVM thread ALWAYS running

霸气de小男生 提交于 2019-12-18 19:06:08
问题 When profiling my application I came across a weird behavior - the DestroyJavaVM thread is ALWAYS running - 100% of the time. After doing a little research on the subject, on which there's hardly any valuable information online, all I understood is that this thread is supposed to unload the JVM upon exit. If that's the case, why is this thread in RUNNING state 100% of the time from the very first moment I start my application? Doesn't it consume valuable resources and therefore may cause an

I want to show gif image show on screen even when app is closed

六眼飞鱼酱① 提交于 2019-12-13 08:50:04
问题 With a mouse click, you implement two things: Sound is playing The GIF image is showing I'm trying to show the GIF on the screen even when I close the app I'm using the services but because the sound keeps playing while the GIF image appears, I can not find the solution. TheService.java (CODE) package gallery.suitapps.catwalking; import android.app.Service; import android.content.Context; import android.content.Intent; import android.media.MediaPlayer; import android.media.SoundPool; import

Java runOnUiThread and Thread.sleep

☆樱花仙子☆ 提交于 2019-12-12 11:23:18
问题 I have this method from a separate class wherein when the call ends, the color of my ImageView changes from red to white. Sample code below: public void endOfCall(){ ((Activity)mContext).runOnUiThread(new Runnable(){ @Override public void run(){ TargetDetails.oncall.setVisibility(View.VISIBLE); TargetDetails.endcall.setVisibility(View.GONE); } }); try{ call.endCall(); }catch (SipException se) {} call.close(); //this is just a representation; not the actual code if(true){ Thread.sleep(10000);

ScheduledExecutorService that interrupts after a timeout

好久不见. 提交于 2019-12-12 02:35:22
问题 I need to implement a scheduled executor service which runs a thread in an interval every x seconds. The thread execution should be interrupted in case it took more than y seconds. I have tried to implement the solution using the ScheduledExecutorService that has a configurable parameter for the interval but does not have one for the timeout. I have a few ideas in mind and I would like to hear your suggestion for implementations/ techniques. 回答1: Does this help?Task begins every 10 seconds

How to understand an example of book java concurrency in practice? [duplicate]

女生的网名这么多〃 提交于 2019-12-12 01:26:25
问题 This question already has answers here : Brian Goetz's improper publication (2 answers) Closed 3 years ago . Listing 3.15. Class at Risk of Failure if Not Properly Published. public class Holder { private int n; public Holder(int n) { this.n = n; } public void assertSanity() { if (n != n) throw new AssertionError("This statement is false."); } } My first question is why javac not optimize if (n != n) ? The following is my demo for the example public class TestSync { private int n; public

Android Images are not getting downloaded (very slow)

泪湿孤枕 提交于 2019-12-11 23:19:10
问题 Actually here I am trying to download the multiple images from server, when I try to download images from localhost it wotks fine (within 3 sec images getting downloaded). But when I try to download it from actual server it is taking so long (atleast 15 sec) even though my internet connection is very fast. Total size of the images are not more than 600 kb. pool = Executors.newFixedThreadPool(SplitImages.length); int length = 0; for (String name : SplitImages) { pool.submit(new

Fixed Thread Pool is exiting immediately, not processing threads

混江龙づ霸主 提交于 2019-12-11 19:25:43
问题 Trying to understand fixed thread pools I made this test code, which revealed the below results, contrary to what I thought it would do: Thread Start: 1 Thread Start: 2 Thread Start: 0 That's it. No "Thread End" messages and only 3 threads were started. I expected and I want all 10 tasks to complete. ExecutorService exec = Executors.newFixedThreadPool(3); for (int c = 0; c < 10; c++) { exec.execute(new TestThread(c)); } exec.shutdown(); public class TestThread implements Runnable { private

CompletableFuture: several tasks

三世轮回 提交于 2019-12-11 16:43:06
问题 How can I asynchronously execute 20 Runnable tasks(or 1 task 20 times), using 5 CompletableFutures? That's what I've got: Runnable task = () -> { long startTime = System.currentTimeMillis(); Random random = new Random(); while (System.currentTimeMillis() - startTime < 3000) { DoubleStream.generate(() -> random.nextDouble()) .limit(random.nextInt(100)) .map(n -> Math.cos(n)) .sum(); } System.out.println("Done"); }; for (int i = 0; i < 4; i++) { CompletableFuture<Void> future1 =