runnable

Lambda casting rules

回眸只為那壹抹淺笑 提交于 2019-12-30 03:05:09
问题 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 回答1: 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

java Runnable run() method returning a value

别说谁变了你拦得住时间么 提交于 2019-12-29 03:17:09
问题 According to java doc, Runnable method void run() cannot return a value. I do wonder however if there is any workaround of this. Actually i have a method which calls: public class Endpoint{ public method_(){ RunnableClass runcls = new RunnableClass(); runcls.run() } } wheren method run() is: public class RunnableClass implements Runnable{ public jaxbResponse response; public void run() { int id; id =inputProxy.input(chain); response = outputProxy.input(); } } I want to have acces to response

Naming threads and thread-pools of ExecutorService

ε祈祈猫儿з 提交于 2019-12-27 10:42:41
问题 Let's say I have an application that utilizes the Executor framework as such Executors.newSingleThreadExecutor().submit(new Runnable(){ @Override public void run(){ // do stuff } } When I run this application in the debugger, a thread is created with the following (default) name: Thread[pool-1-thread-1] . As you can see, this isn't terribly useful and as far as I can tell, the Executor framework does not provide an easy way to name the created threads or thread-pools. So, how does one go

Multiple Runnable in a single thread -java

南笙酒味 提交于 2019-12-25 18:38:48
问题 I am trying to have a bunch of runnable threads that can be started one at a time. Something like First(new Thread() { public void run() { //do something } }); Is what I'm trying to do impossible? 回答1: You can use a single threaded Executor ExecutorService service = Executors.newSingleThreadedPool(); service.submit(runnable1); service.submit(runnable2); service.submit(runnable3); 回答2: Yes, just have multiple private methods: public class FirstCaller { private void method1() { } private void

Using Runnable to run a method by multiple threads

雨燕双飞 提交于 2019-12-25 16:49:02
问题 In my program, I want to create multiple threads in one of the methods where each thread has to run a specific method with a given input. Using Runnable , I have written this snippet. class myClass { public myClass() { } public void doProcess() { List< String >[] ls; ls = new List[2]; // two lists in one array ls[0].add("1"); ls[0].add("2"); ls[0].add("3"); ls[1].add("4"); ls[1].add("5"); ls[1].add("6"); // create two threads Runnable[] t = new Runnable[2]; for (int i = 0; i < 2; i++) { t[ i

ScheduledExecutorService - Ignore already running runnable

谁说胖子不能爱 提交于 2019-12-25 15:40:10
问题 I'm using a scheduled executor service private ScheduledExecutorService pool = new ScheduledThreadPoolExecutor(1); running a runnable at fixed rate pool.scheduleAtFixedRate(new CoolRunnable(), 10, 10, TimeUnit.MILLISECONDS); This thread pool waits that the previous execution finishes, but i'd like it to run the runnable every 10 milliseconds no matter whether the previous is done or not. How can I do that? EDIT: Fixed the problem replacing the MySQL connection with a connection pool. The

URL Parsing with Java server using Runnable class

廉价感情. 提交于 2019-12-25 08:57:11
问题 How can I parse URL queries with a system like this. For Example something like get these URL arguments in variables. http://localhost?format=json&apikey=838439873473kjdhfkhdf http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html I made these files WorkerRunnable.java package servers; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.net.Socket; /** */ public class WorkerRunnable implements Runnable{ protected Socket

Android: When to use Button.Post?

依然范特西╮ 提交于 2019-12-25 07:47:24
问题 I noticed a method called Post in Android's Button class. I was wondering what this method was for? This method takes a runnable argument, my question is when should we use this vs just having a listener bind to the click event of this button? What is the difference? Any example is appreciated. 回答1: post() is inherited from View . It has nothing to do with button clicks and is not a replacement for an OnClickListener . post() is used to arrange for a Runnable to be executed on the main

How to keep active a runnable thread when an activity is closed but destroy the thread when the activity start again

旧街凉风 提交于 2019-12-25 05:56:05
问题 I have a runnable thread in one of my activities, which starts when the activity starts. I want to keep the thread running even when my activity is finished, and I want to destroy the thread when the same activity starts again. Is this possible or do I have to try new approach to achieve my goal? 回答1: I would suggest using a service. They live as long as you want them to public class MyService extends Service { private static final String TAG = "MyService"; @Override public IBinder onBind

Android threading and Handler not working

倾然丶 夕夏残阳落幕 提交于 2019-12-25 02:17:56
问题 I recently refactored an old project and found that a particular portion no longer wants to run properly no matter what I do. Essentially, I have an Activity with a TextView. This view is updated at timed intervals from a thread invoked within the same class. The pause is accomplished with a Thread.sleep and a Handler is used to trigger the update of the UI. The thing is, now I either get a CalledFromWrongThreadException saying that I cannot manipulate the View from another thread, a long