Hot to use Thread in JAVA for EWS API? - java

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.
}

Related

Progress bar over two Threads

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();
}
});

Bukkit runnable tips

Hello any ideas how to make scheduleSyncDelayedTask so it doesnt cancel previous task, which should be running and canceled a bit later?
for(int x = 0; x < 8; x++){
int taskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.getPlugin(), new Runnable() {
#Override
public void run() {
CreateItems.createItemsOnStand2(player, bedna, listitems);
}
}, 30*x , 2+x);
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getPlugin(), new Runnable() {
public void run() {
Bukkit.getScheduler().cancelTask(taskID);
}
}, (x==0) ? 30 : 30*x);
}
You can use BukkitRunnables for task scheduling
new BukkitRunnable() {
#Override
public void run() {
//Code you need running
this.cancel(); //Cancelling
}
}.runTaskTimer(pluginInstance, delayTime, repeatingTime);
new BukkitRunnable() {
#Override
public void run() {
//Code you need running
this.cancel(); //Cancelling
}
}.runTaskLater(pluginInstance, delayTime);
It makes it so the task can be easily created and self-cancellable

Threads running at specific time intervals for a specific time in JAVA

I want to use the below mentioned operations in JAVA for android development.
For 30 Seconds ,Run a Function F1() every 1 second (resulting in 30 F1 calls).
Run a Thread t1 forever
The above steps should execute sequentially.
I Have tried with ExecutorServicebut with no success.
This is my code for reference
final Handler h = new Handler();
final int delay = 1000; //milliseconds
ExecutorService executor = Executors.newFixedThreadPool(1);
Thread t1 = new Thread(new Runnable() {
#Override
public void run() {
F1();
}
});
for(int i=0;i<30;i++){
executor.submit(t1);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
executor.shutdown();
//Step 2 (THe Second Thread)
h.postDelayed(new Runnable() {
public void run() {
AnotherFunction()
h.postDelayed(this, delay);
}
}, delay);
Generally, ExecutorService is more preferable for such operations. Here is a good post describing the differences and features of Timer and ExecutorService.
As for your question directly - it can be implemented in such way:
// here are Runnables with test logic
Runnable foo = new Runnable() {
#Override
public void run() {
Log.d(">>>", "foo");
onTaskFinished();
}
};
Runnable longRunning = new Runnable() {
#Override
public void run() {
try {
Log.d(">>>", "longRunning started");
Thread.sleep(5000);
Log.d(">>>", "longRunning finished");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
// and here is valuable logic
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
ScheduledFuture<?> schedulerHandler;
volatile AtomicInteger tasksNum = new AtomicInteger(0);
private synchronized void onTaskFinished(){
if(tasksNum.incrementAndGet() >= 30){
scheduler.execute(new Runnable() {
#Override
public void run() {
schedulerHandler.cancel(true);
}
});
scheduler.execute(longRunning);
}
}
And then to start operation just invoke this command:
schedulerHandler = scheduler.scheduleAtFixedRate(foo, 0, 1, TimeUnit.SECONDS);
You may consider using the java.util.Timer and java.util.TimerTask classes
If you're doing what I think you're doing you can do as #erosb hinted, use Timer and TimerTask to schedule method executions at a fixed rate.
The following should work for you.
final int DELAY_BEFORE_START = 0;
final int RATE = 1000;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
F1();
}
}, DELAY_BEFORE_START, RATE);

Cannot name background thread, Void error

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

Java 8 Timer to repeat one function

How can I implement a timer in Java 8? I prefer one simple method for this. I want to do something every 15 min or 30 min. Any idea?
you can use
Thread.sleep(milliseconds)
call the function you want and put it inside Runnable .
Example :
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(3000); // 3
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
#Override
public void run() {
//your Function
}
});
}
}).start();
OR
Thread t = new Thread(new Runnable() {
public void run() {
// stuff here
}});
t.start();

Categories