runnable

Parsing @Context UriInfo to java thread

隐身守侯 提交于 2019-12-01 14:10:42
I'm trying to parse @Context UriInfo to another thread and do some task. But when i try to run it, it gives the error as Exception in thread "Thread-691" org.jboss.resteasy.spi.LoggableFailure: Unable to find contextual data of type: javax.ws.rs.core.UriInfo My code as follows @GET @Path("/thread") public void thread(@Context UriInfo url){ Runnable run = new Runnable() { @Override public void run() { System.out.println(">>>>>>>>>>>> " + url.getRequestUri().getQuery()); } }; Thread t = new Thread(run); t.start(); } How can I get the UriInfo to the new thread? Just make the UriInfo parameter

Java多线程基础篇(02)-多线程的实现

99封情书 提交于 2019-12-01 05:29:15
1.概要 JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没有返回值,只有最后一种是带返回值的。本节,我们将探讨常用的实现多线程的2种方式:Thread和Runnable。至于第三种ExecutorService、Callable、Future的实现是属于java.util.concurrent包下的内容,我将单独设置一节去探讨。 2.Runnable简介 2.1 Runnable概述 Runnable是一个接口,该接口只包含了一个run无参方法。设计该接口的目的是为希望在活动时执行代码的对象提供一个公共协议。例如,Thread类实现了Runnable。激活的意思是说某个线程已经启动并且尚未停止。 此外Runnable为非Thread子类的类提供了一种激活方式。通过实例化某个Thread实例并将自身作为运行目标,就可以运行实现Runnable的类而无须创建Thread的子类。大多数情况下,如果只想重写run()方法,而不重写其他Thread方法,那么应使用Runnable接口。 这很重要,因为除非程序猿打算修改或增强类的基本行为,否则不应该创建子类。 2.2 Runnable源码 package java.lang; public

Add properties file to build path of runnable jar

£可爱£侵袭症+ 提交于 2019-12-01 01:22:44
is it possible to add to the classpath of a runnable jar file some properties file? I tryed these solutions solutions: running the executable file using the following command: java -cp ../prop_dir/prop1.properties;../prop_dir/prop2.properties -jar MyRunnableJar.jar adding to the MANIFEST FILE (in the Class-Path section) ../prop_dir/prop1.properties ../prop_dir/prop1.properties but none of them works. The architecture of the running dir is the following + + MyRunnableJar.jar + prop_dir/ + prop1.properties + prop2.properties Thanks so much, Daniele EDIT When I execute the following line System

Android: Which thread calls .onSensorChanged?

◇◆丶佛笑我妖孽 提交于 2019-11-30 20:47:36
I've read a few discussions about which thread calls various callback methods, for example those associated with Sensors. Most claim that the UI thread calls the callbacks - even when a separate worker thread is involved. Are we CERTAIN about that? Consider this scenario: A separate class implements Runnable and SensorListener. The UI thread (during onCreate) starts the runnable and then goes back to its other business. The now-independent worker thread, in its own class, then registers the SensorListener. Note that the UI thread never has any interaction with SensorManager nor SensorListener.

Playback video in slow motion in android

不羁的心 提交于 2019-11-30 10:02:56
- I am working on a project which needs to play video in slow motion. - I am well aware that Android doesn't provide these functionality. - I found PVPlayer Engine and libVLC which possessed these capabilities, but i didn't found any tutorial or proper documentation of including them in the android project and using them. - So i tried doing this by using Runnable and Handler , it was successful in slowing down the video but they possessed jerks during playing. public class MainActivity extends Activity { VideoView vx; Button mbutt; Handler h ; int curr = 0; @Override protected void onCreate

Lambda casting rules

别说谁变了你拦得住时间么 提交于 2019-11-30 08:48:23
I was curious why a lambda with a return type can not be casted to a Runnable whereas a non void method reference can. Runnable r1 = () -> 1; // not allowed // error: incompatible types: bad return type in lambda expression // int cannot be converted to void Runnable r2 = ((Supplier)() -> 1)::get; // allowed The Runnable interface defines the run method with return type void. In a lambda expression that means that the part following the arrow -> must be a statement . This is explained in JLS §15.27.3 : If the function type's result is void, the lambda body is either a statement expression (§14

Android: Which thread calls .onSensorChanged?

时光毁灭记忆、已成空白 提交于 2019-11-30 04:56:38
问题 I've read a few discussions about which thread calls various callback methods, for example those associated with Sensors. Most claim that the UI thread calls the callbacks - even when a separate worker thread is involved. Are we CERTAIN about that? Consider this scenario: A separate class implements Runnable and SensorListener. The UI thread (during onCreate) starts the runnable and then goes back to its other business. The now-independent worker thread, in its own class, then registers the

How to replace HashMap Values while iterating over them in Java

假如想象 提交于 2019-11-30 02:38:43
I am using a Runnable to automatically subtract 20 from a players cooldown every second, but I have no idea how to replace the value of a value as I iterate through it. How can I have it update the value of each key? public class CoolDownTimer implements Runnable { @Override public void run() { for (Long l : playerCooldowns.values()) { l = l - 20; playerCooldowns.put(Key???, l); } } } Using Java 8: map.replaceAll((k, v) -> v - 20); Using Java 7 or older: You can iterate over the entries and update the values as follows: for (Map.Entry<Key, Long> entry : playerCooldowns.entrySet()) { entry

Updating GUI: Runnables vs Messages

你说的曾经没有我的故事 提交于 2019-11-29 20:51:09
To update the GUI from other threads, there are basically two main approaches: Use java.lang.Runnable with any of these methods: Activity.runOnUiThread(Runnable) View.post(Runnable) View.postDelayed(Runnable, long) Handler.post(Runnable) Use android.os.Message: Handler.sendMessage(Message) / Handler.handleMessage(Message) You can also use AsyncTask, but my question is more focused on the use case of updating a very simple component. Let's see how it would be done using both approaches: Using Runnables: TextViev tv = ...; final String data = "hello"; Runnable r = new Runnable() { @Override

Given two Java threads, stop one thread when one of them finishes

会有一股神秘感。 提交于 2019-11-29 14:38:29
I'm looking for a clean design/solution for this problem: I have two threads, that may run as long as the user wants to, but eventually stop when the user issues the stop command. However if one of the threads ends abruptly (eg. because of a runtime exception) I want to stop the other thread. Now both threads execute a Runnable (so when I say 'stop a thread' what I mean is that I call a stop() method on the Runnable instance), what I'm thinking is to avoid using threads (Thread class) and use the CompletionService interface and then submit both Runnables to an instance of this service. With