I am trying to name my thread, I have this code
public void DownloadFromUrl(final String fileName) { //this is the downloader method
new Thread(new Runnable() {
public void run() {
Looper.prepare();
...
but when I try to name it like this
public void DownloadFromUrl(final String fileName) { //this is the downloader method
Thread t1 = new Thread(new Runnable() {
public void run() {
Looper.prepare();
...
it just says
Required: Java.lang.Thread
Found: Void
Maybe you called the start method on the thread. This returns void.
Try this instead.
Thread t1 = new Thread(new Runnable() {
public void run() {
Looper.prepare();
...
}
t1.start();
But I agree with the other answer, you probably should use somethine else other than threads.
try to use AsynkTask for downloading instead of thread
Look as this AsynkTask
Related
I'd like to have a kind of overall progress bar for two parallel running Thread instances.
Unfortunately I'm not able to make it. Can somebody give me any hint?
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
loadItems(Parent);
}
});
t.start();
Thread r = new Thread(new Runnable() {
#Override
public void run()
{
loadItems(Parent2);
}
});
r.start();
dmf.getFrame().dispose();
}
});
In java and android world, to run now Thread we execute it like this
Thread tr = new Thread(new Runnable() {
#Override
public void run() {
todo();
}
});
tr.start();
so tr is new Thread, but in android api PerformClick implements Runnable interface and is called normally by execute run method
private static void handleCallback(Message message) {
message.callback.run();
}
callback is PerformClick
private final class PerformClick implements Runnable {
#Override
public void run() {
performClick();
}
}
I am trying understand why for this case it is not new Thread
Thanks
I have below function for EWS JAVA API.
public static void cleanRootFolders(String account) throws Exception{
deleteEmailsFromInbox(account);
deleteEmailsFromDrafts(account);
deleteEmailsFromSentItems(account);
deleteEmailsFromJunkEmails(account);
deleteEventsFromCalendar(account);
deleteEmailsFromDeletedItems(account);
}
How can i Implement Thread for performing this Six methods simultaneously for saving the time instead of one after one ?
You can use a thread pool as follows:
ExecutorService executor = Executors.newFixedThreadPool(6);
Runnable r = new Runnable() {
#Override
void run() {
deleteEmailsFromInbox(account);
}
}
executor.execute(r)
r = new Runnable() {
#Override
void run() {
deleteEmailsFromDrafts(account);
}
}
executor.execute(r)
Or you could simply start a thread for each of the tasks:
Runnable r = new Runnable() {
#Override
void run() {
deleteEmailsFromInbox(account);
}
}
(new Thread(r)).start();
Below is complete code:
public static void cleanRootFolders(String account) throws Exception{
/*create number of threads = number of cores. Do note, creating 6 threads doesn't mean 6 threads will work simultaneously.*/
ExecutorService executor = Executors.newFixedThreadPool(numberOfCores);
executor.execute(new Runnable() {
public void run() {
deleteEmailsFromInbox(account);
}
});
executor.execute(new Runnable() {
public void run() {
deleteEmailsFromDrafts(account);
}
});
executor.execute(new Runnable() {
public void run() {
deleteEmailsFromSentItems(account);
}
});
executor.execute(new Runnable() {
public void run() {
deleteEmailsFromJunkEmails(account);
}
});
executor.execute(new Runnable() {
public void run() {
deleteEventsFromCalendar(account);
}
});
executor.execute(new Runnable() {
public void run() {
deleteEmailsFromDeletedItems(account);
}
});
executor.shutdown();
//always shutdown, so the threads do not keep running.
}
How can we mult-thread in libgdx ?
I tried:
new Thread(new Runnable() {
#Override
public void run() {
// do something important here, asynchronously to the rendering thread
final int result = 10;
// post a Runnable to the rendering thread that processes the result
Gdx.app.postRunnable(new Runnable() {
#Override
public void run() {
Array<Integer> results = new Array<Integer>;
Gdx.app.log("Thread1", "Worked");
results.add(result);
}
});
}
}).start();
I have read here and it is official. But it did not tell me specifically how to access the array.
How can I access the result? through postRunnable
You could subclass Runnable and keep the array as a member in that new subclass, there is a nice explanation for that approach here https://stackoverflow.com/a/7762490/4802055
In java this is valid
new Thread(new Runnable()
{
public void run()
{
for(int i=0;i<5;i++)
System.out.println("From anonymous:"+i);
}
}
).start();
But this is not :
Thread t=new Thread(new Runnable()
{
public void run()
{
for(int i=0;i<5;i++)
System.out.println("From anonymous:"+i);
}
}
).start();
can I achieve it with anonymous class? If yes then How
Your code does not work, because it wants to assign the result of the start() method to the variable t. You can do it like so:
Thread t=new Thread(new Runnable()
{
public void run()
{
for(int i=0;i<5;i++)
System.out.println("From anonymous:"+i);
}
}
);
t.start();
Also, in this case you don't need to use Runnable interface because is implemented by the Thread class.
new Thread() {
#Override
public void run() {
for(int i=0;i<5;i++)
System.out.println("From anonymous:"+i);
}
}.start();
One thing to note here is that the start method of Thread returns void. This is why you cannot assign it to a variable.