Skip to content

Commit 40e64bc

Browse files
authored
Update java开发以太坊教程.md
1 parent 01452f0 commit 40e64bc

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

java开发以太坊教程.md

+19-4
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ gas limit 是gas price的基础上 你最多愿意出多少份,也就是说gas
306306
Credentials credentials = Credentials.create(privateKey );
307307

308308
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
309-
from, DefaultBlockParameterName.LATEST).sendAsync().get();
309+
from, DefaultBlockParameterName.LATEST).send();
310310

311311
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
312312

@@ -650,9 +650,24 @@ allowance 两个参数_owner ,_spender 查询剩余的_owner 授权给_sp
650650
同步就是要等待处理完才继续执行,如先approve再进行transferFrom,如果使用异步就可能出问题,还没授权成功就进行交易,这样首先不会成功,而且gas price * gas limit会被全吞,本文使用的全都是send(),这样会带来一个问题,接口等待时间太长,响应无效了,处理方式如下
651651
调用上链操作时异步执行
652652
653-
Async.run(() ->
654-
proxyTranUsdt(address,address,num)
655-
);
653+
// 异步执行上链
654+
// 两个线程的线程池
655+
ExecutorService executor = Executors.newFixedThreadPool(2);
656+
//jdk1.8的实现方式
657+
CompletableFuture<String> future = CompletableFuture.supplyAsync(new Supplier<String>() {
658+
@Override
659+
public String get() {
660+
System.out.println("task started!");
661+
try {
662+
proxyTranUsdt(address,address,num) ;
663+
} catch (Exception e) {
664+
e.printStackTrace();
665+
}
666+
return "task finished!";
667+
}
668+
}, executor);
669+
//采用lambada的实现方式
670+
future.thenAccept(e -> System.out.println(e + " ok"));
656671
657672
## 15.分享一下我的上链记录class
658673

0 commit comments

Comments
 (0)