程序:可执行的软件。
进程:正在运行的程序,是线程的集合。
线程: 独立运行的执行路径。
使用线程可以把占据时间长的程序中的任务放到后台去处理,程序的运行速度可能加快,在一些等待的任务实现上如用户输入、文件读写和网络收发数据等,线程就比较有用了。在这种情况下可以释放一些珍贵的资源如内存占用等等。
如果有大量的线程,会影响性能,因为操作系统需要在它们之间切换,更多的线程需要更多的内存空间,线程的中止需要考虑其对程序运行的影响。通常块模型数据是在多个线程间共享的,需要防止线程死锁情况的发生。
总结:进程是所有线程的集合,每一个线程是进程中的一条执行路径。
线程之间是独立运行的。
多线程:提高程序执行效率。异步执行
创建线程
1、继承Thread ,重写run方法
class CreateThread extends Thread {
// run方法中编写 多线程需要执行的代码
publicvoid run() {
for (inti = 0; i< 10; i++) {
System.out.println("i:" + i);
}
}
}
publicclass ThreadDemo {
publicstaticvoid main(String[] args) {
System.out.println("-----多线程创建开始-----");
// 1.创建一个线程
CreateThread createThread = new CreateThread();
// 2.开始执行线程 注意 开启线程不是调用run方法,而是start方法
System.out.println("-----多线程创建启动-----");
createThread.start();
System.out.println("-----多线程创建结束-----");
}
}
2、实现runnable接口,重写run方法
class CreateRunnable implements Runnable {
@Override
publicvoid run() {
for (inti = 0; i< 10; i++) {
System.out.println("i:" + i);
}
}
}
publicclass ThreadDemo2 {
publicstaticvoid main(String[] args) {
System.out.println("-----多线程创建开始-----");
// 1.创建一个线程
CreateRunnable createThread = new CreateRunnable();
// 2.开始执行线程 注意 开启线程不是调用run方法,而是start方法
System.out.println("-----多线程创建启动-----");
Thread thread = new Thread(createThread);
thread.start();
System.out.println("-----多线程创建结束-----");
}
}
3、匿名内部类
第三种使用匿名内部类方式
System.out.println("-----多线程创建开始-----");
Thread thread = new Thread(new Runnable() {
public void run() {
for (int i = 0; i< 10; i++) {
System.out.println("i:" + i);
}
}
});
thread.start();
System.out.println("-----多线程创建结束-----");
4、实现callable接口,重写call方法(可以获得返回值)
线程池调用
ExecutorService exec = Executors.newFixedThreadPool(wallets.size());
List<Callable<LBallanceVO>> tasks = new ArrayList<>();
for (int i=0; i< wallets.size(); i++) {
workThread work = new workThread(wallets.get(i));
tasks.add(work);
}
List<Future<LBallanceVO>> results = null;
try {
results = exec.invokeAll(tasks);
exec.shutdown();
} catch (Exception e) {
logger.error("Thread err :",e);
return JsonUtil.toJsonError(ERROR_21333);
} finally {
if(exec != null)
exec.shutdown();
}
for (int i = 0; i < results.size(); i++){
LBallanceVO bal = null;
try {
bal = results.get(i).get();
} catch (Exception e) {
logger.error("Thread err :",e);
return JsonUtil.toJsonError(ERROR_21333);
}
}
private class workThread implements Callable<LBallanceVO> {
private Wallet wallet;
public workThread(Wallet wallet) {
this.wallet = wallet;
}
@Override
public LBallanceVO call() throws Exception {
ChainService service = getChainService(wallet.getChainId(),wallet.getAppId());
BalanceVO bal = new BalanceVO();
bal.setAddress(wallet.getPublicKey());
bal.setChainId(wallet.getChainId());
List<SubChain> subChains = walletCoinMapper.getOwnCoinByWalletId(wallet.getWalletId(),wallet.getAppId());
for (SubChain subChain : subChains) {
if(!StringUtils.isEmpty(subChain.getTokenAddress())) {
bal.getTokens().add(subChain.getTokenAddress());
}
}
bal.setAppId(wallet.getAppId());
LBallanceVO res = service.getBallance(bal);
res.setClassName(service.getClass().getSimpleName());
return res;
}
}
第二种:
FutureTask futureTask=new FutureTask(new Callable() {
@Override
public String call() throws Exception {
Thread.sleep(3000);
System.out.println("calld方法执行了");
return "call方法返回值";
}
});
new Thread(futureTask).start();
System.out.println("获取返回值: " + futureTask.get());