What is a Runnable (Java) counterpart in Objective-C? - java

What is a good way to schedule tasks on the Main thread in Objective-C? Does any of the following ways create a new thread?
performSelector afterDelay
dispatch_after

Neither creates a new thread. the two just schedule a call on the current NSRunLoop.
GCD uses an internal thread pool and dispatch_async schedules a call on any of these threads. That is one 'counterpart' for a Runnable (when used with a Thread object / a Threadpool).
Another 'legacy' way is NSThread's detachNewThreadWithSelector or performSelectorInBackground! :)

Related

How to run a delayed or scheduled task in Java provided you have a thread?

Suppose I have a thread from a thread pool. How to execute delated/scheduled task (Runnable or Callable) in this thread?
Don't use a Thread directly for that.
Use, for instance, a ScheduledExecutorService.
The Executors class is probably what you want to create one.

Callable Threads versus Runnable Threads versus Extend Thread

I was recently reading about creation of Threads in java by implementing Runnable or Extending thread and last Implementing Callable. Runnable Versus Callable thread at stackoverflow describes the difference quoting both are designed for classes whose instances are potentially executed by another thread. What does it mean? Does it creates new Thread? If yes, why we need to pass a class that implements Runnable to Thread constructor?
Also, i saw the method of creating threads by implementing Runnable or Extending thread. In the first method, (in the tutorials what i found), we need to call Thread class which requires the Runnable instance to start the thread. But, i could not found the similar thing for Callable as there is no Thread constructor which accepts Callable. Executor framework or Future Task is used for the purpose of running those threads. Then why we say both ways are same (except Callable retruns something and can throw Exception).
Last, is writing
Thread t = new Thread();
Thread t1 = new Thread(new RunnableInstance());
Do these create new Threads in system? Is there any other way of using Runnable to create new threads without passing it as a constructor to Thread class?
It should not be a duplicate question.
What does it mean? Does it creates new Thread?
Both Callable and Runnable are just interfaces, they don't create any threads by themself. Instead they provide API and abstractions for developers. When you want to execute some code in separate thread you typically implement Runnable and then you can decide how to execute this. It is not bound to any thread yet. You have many options actually:
Execute it in the new Thread
Execute it with some ExecutorService
Or just call it directly
If yes, why we need to pass a class that implements Runnable to Thread constructor?
No.
Since Runnable does not create thread behind (well, it simply can't since it's just an interface!), we need to execute this Runnable explicitly.
Do these create new Threads in system?
Yes.
Is there any other way of using Runnable to create new threads without passing it as a constructor to Thread class?
Yes. I mentioned already ExecutorService. You can profit from thread pool or completion service, take a look at the API and examples.
Callable
It will return the result of the execute.
Runnable
It wont return. But it will run separately like callable.
Extends Thread
Its also a runnable. But if you extend thread , you cant extend any class since java wont support multiple inheritance.
Callable and Runnable provides interfaces for other classes to execute them in threads. They contain no functionality of their own. The most common way to do this is via an ExecutorService. Have a look at the classes available in java.until.concurrent. There are many options there. Extending Thread is not really called for unless you really intend to add new low-level threading functionality.

Queues for every thread in thread pool

As far I know, thread pools (java.util.concurrent.Executor class) provide a queue of tasks for all threads in a pool. So I don't really know, which thread will execute my task. But I need to have queues of tasks assigned to every thread. How can I do it?
If you want only certain threads to execute certain tasks you a standard Threadpool will not fit.
But you can use multiple Threadpools with only one thread in each to solve your problem.
You should write your program so you don't need to know what thread executes a task. They are just anonymous worker threads.
However, if you really want to know anyway you can create a single threaded ExecutorService for each thread you want and then you will know which thread will execute a task.

Dispatch a task from EDT to main?

I've been reading a bit about concurrency (which gives me a headache).
I understand you can set a task to run on the EDT from the main thread using:
SwingUtilities.invokeLater
but can you set a task to run on the main thread from the EDT?
As in:
Thread mymainthread=Thread.currentThread();//<referring to the thread that initially kicks off public static void main
public void mousePressed(MouseEvent e){ //queue a task to run on mymainthread }
Can it be done? Is it a bad idea?
The other question on SO similiar to this (here) talks about creating a new thread but wouldn't it be safer and simpler to keep using the main if I was aiming for a single thread (+EDT) application? .......or maybe I've got this all wrong.
EDIT: What I should have explained: I wanted to create objects that would communicate with each other on the main thread (running in a slow loop) so I didn't want any of them be instantiated on a different thread, edt or otherwise.
but can you set a task to run on the main thread from the EDT?
I think you are confused on what EDT is. Swing and many other frameworks use a technique called thread-confinement.
In order to guarantee thread-safety, all actions are executed from a single thread. This thread in Swing is called Event Dispatcher Thread.
This thread has a queue and executes all tasks from that queue sequentially one at a time, at the same thread. This is why your tasks should be short in order not to block the UI.
So when you use EDT you are essentially passing a task to its queue from your thread and EDT will eventually execute it.
What you can do is put a task on the EDT queue which spawns a thread to be executed on separate thread. If you want to use your current thread for some reason as the background thread perhaps you could but why would you need that? The most straightforward way is just to submit a runnable to run as part of some background thread e.g. part of a pool
You can create your own event loop to do thread-confinement. This would allow you a separate single thread which would behave like the EDT. Be careful not to share [effectively] mutable objects between the two threads simultaneously.
Implementation can be as simple as a while loop with a BlockingQueue. You can go slightly higher level by getting an ExecutorService from java.util.concurrent.Executors.newFixeThreadPool(1).

android runnable difference

What is the different b/w running runnable inside handler new Handler().post(runnable) and running in Thread(runable) ?
Handler is used for communication between and coordinating threads. By creating a Handler, you bind it to the current thread. If you post a runnable to that Handler, it will be executed in that same thread.
Thread is Java's way to spawn new user-level threads. The runnable you pass it will be executed in that thread.
The two concepts are not mutually exclusive. You can use Handler with custom Threads.

Categories

Resources