I am using the following codes, to replace a JLabel each 0.5 seconds, with the same sentence but with another dot.
Runnable r1=new Runnable() {
@Override
public void run() {
while(true){
try {
connectionStatus.setText("Connection Established...");
Thread.sleep(500L);
connectionStatus.setText("Connection Established.");
Thread.sleep(500L);
connectionStatus.setText("Connection Established..");
Thread.sleep(500L);
} catch (InterruptedException ex) {
}
}
}
};
Thread th1=new Thread(r1);
th1.start();
Is this the real purpose from using threads? Does this affect the speed of the program? If the thing that I’m doing is so stupid, is there any other way to do such stupid things?
If you want these operations to run in parallel then the answer is yes
If you update your label at a fixed intervals then you should probably go for using Timers instead
On a side note:
while(true)when doing multithreading, you should either define a stop boolean that you test in the loop or useThread.interrupted()if you extend theThreadclassSwingUtilities.invokeLater