Progress bar over two Threads - java

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

Related

Hot to use Thread in JAVA for EWS API?

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

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

Updating the progress bar

I have a big program which needs to be called by a GUI. The GUI has a progress bar which needs to be updated(like 5% .... 10% )after the user presses the start button.
The problem is that the background task performed does not have a fixed execution time. So is somehow possible to measure the progress of the task performed in the doInBackground() method (i am trying to use SwingWorker). Or should i go with an indeterminate progress bar.
I was unable to clearly understand the example provided on Oracle's tutorial page and wasn't able to find a decent page explaining how to use a progress bar.
Any help will be highly appreciated.
According to the problem, I would use a infinite progress bar
public class Indeterminate extends JProgressBar {
private static final long serialVersionUID = -281761375041347893L;
/***
* initial the ProgressBar
*/
public IndeterminateProgressBar() {
super();
setIndeterminate(true);
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
setVisible(false);
}
});
}
/**
* call this, if you start a long action
*/
public void start() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
setVisible(true);
}
});
}
/**
* if you have finished, call this
*/
public void end() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
setVisible(false);
}
});
}
}
Used like this:
ActionListener startButtonListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
#Override
public void run() {
try {
progressBar.start();
// long operation
} catch (Exception e) {
// handle excpetion
} finally {
progressBar.end();
}
}
}).start();
}
};

timer exit condition in java

String move=jTextField1.getText();
i=Integer.parseInt(move);
timer = new Timer(1000,new ActionListener(){
public void actionPerformed(ActionEvent e)
{
i--;
if(i<=0)
{
if(move.equals("0"))
{
Thread th=new Thread(new DetectImage());
th.start();
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
new TrafficMainGUI(storeValue);
}
});
}
timer.stop();
}
}
jTextField1.setText(""+i);
}
});
timer.start();
move=""+i;
//Thread th in DetectImage class
public void run()
{
while(stay<20)
{
try {
stay++;
//few contions
Thread.sleep(1000);
}
}
}
//EveryThing is working fine with thread but when i use SwingUtiities.invokeLater()
to call the same class in which this code is there for getting infinite condition.
This doesnot redirect it to the class TrafficMainGUI.Is there some other method to achieve this kind of model.
jTextField1.setText(""+i); must be wrapped in invokeLater for this job by invoked from util.Timer
use Swing Timer instead
if(move.equals("0")) { is about animations, then to use Swing Timer exclusivelly

Categories