Home  >  Article  >  Java  >  What is the difference between asynchronous and synchronous in java

What is the difference between asynchronous and synchronous in java

WBOY
WBOYforward
2023-05-10 21:01:12855browse

Difference

1. The difference between asynchronous and synchronous is generally whether the caller needs to wait for the result.

2. Do you need to wait for the result to be synchronized?

3. You can also decide whether you need to wait for the result to be asynchronous.

Example

--异步
Thread t3 = new Thread(new Runnable() {
 
@Override
public void run() {
try {
 
for (int i = 0; i < 10; i++) {
 
Thread.sleep(1100);
 
System.out.println("4:"
+ i);
}
} catch (Exception e) {
e.printStackTrace();
}
 
}
});t3.start();
 
--同步
SwingUtilities.invokeAndWait(new Runnable() {
 
@Override
public void run() {
// TODO Auto-generated method stub
 
}
});
 
t3.isAlive()

The above is the detailed content of What is the difference between asynchronous and synchronous in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete