我正在尝试为测试目的创建100k以太坊钱包。所有人都应该使用相同的密码短语,因为它现在无关紧要。我用10个线程启动了这个代码,它冻结了我的macbook,我不得不重新启动它。 3个线程有点工作,但它仍然很慢(每秒生成约6个钱包)。怎么了?我正在使用 web3j 依赖
public EthWallets(String[] args)
{
File destinationDir = new File("ethwallets/");
if(args[1].equalsIgnoreCase("create")) {
try {
int count = Integer.valueOf(args[2]);
if(count > 10)
{
final int threadedCount = count / 10;
for(int i = 0; i < 10; i++)
{
Thread t = new Thread()
{
@Override
public void run() {
for(int j = 0; j < threadedCount; j++)
{
create("passphph", destinationDir);
}
}
};
t.start();
}
}
else
for(int i = 0; i < count; i++)
create("passphph", destinationDir);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void create(String passPhrase, File destinationDirectory)
{
try {
String path = WalletUtils.generateFullNewWalletFile(passPhrase, destinationDirectory);
// Credentials credentials = WalletUtils.loadCredentials(passPhrase, new File(path));
// System.out.println("address: " + credentials.getAddress());
// System.out.println("private: " + credentials.getEcKeyPair().getPrivateKey());
// System.out.println("public: " + credentials.getEcKeyPair().getPublicKey());
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:-1)
我建议你阅读https://stackoverflow.com/a/1718522/5632150
的回复正如他所说,您可以生成的线程数取决于您的线程执行或不执行任何I / O操作的事实。如果是这样,有一些方法可以优化此问题。如果不是,我通常会做MAX_THREADS = N_CORES + 1。