Added multithreading
This commit is contained in:
@@ -1,16 +1,25 @@
|
||||
import model.BlockChain;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.stream.IntStream;
|
||||
import model.BlockChain;
|
||||
import model.Miner;
|
||||
|
||||
public final class Main {
|
||||
public static void main(final String[] args) {
|
||||
public static void main(final String[] args) throws InterruptedException {
|
||||
final var sc = new Scanner(System.in);
|
||||
System.out.print("Enter how many zeros the hash must start with: ");
|
||||
final var hashZeroes = sc.nextInt();
|
||||
final var blockChain = new BlockChain(hashZeroes);
|
||||
blockChain.load();
|
||||
|
||||
IntStream.range(0, 5).forEach(block -> blockChain.addBlock());
|
||||
final List<Miner> miners = new ArrayList<>();
|
||||
IntStream.range(0, 5).forEach(minerId -> miners.add(new Miner(minerId, blockChain)));
|
||||
miners.forEach(Thread::start);
|
||||
|
||||
for (final Miner miner : miners) {
|
||||
miner.join();
|
||||
}
|
||||
|
||||
System.out.println(blockChain);
|
||||
blockChain.save();
|
||||
|
||||
@@ -11,6 +11,7 @@ public class Block implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private final String previousBlockHash;
|
||||
private final int minerId;
|
||||
private final int id;
|
||||
private final int hashZeroes;
|
||||
private final long timeStamp;
|
||||
@@ -19,8 +20,9 @@ public class Block implements Serializable {
|
||||
private int magicNumber;
|
||||
private float generationSecs = 0;
|
||||
|
||||
public Block(final String previousBlockHash, final int id, final int hashZeroes) {
|
||||
public Block(final String previousBlockHash,final int minerId, final int id, final int hashZeroes) {
|
||||
this.previousBlockHash = previousBlockHash;
|
||||
this.minerId = minerId;
|
||||
this.id = id;
|
||||
this.hashZeroes = hashZeroes;
|
||||
this.timeStamp = new Date().getTime();
|
||||
@@ -53,6 +55,7 @@ public class Block implements Serializable {
|
||||
|
||||
public String toString() {
|
||||
return "Block: " + "\n"
|
||||
+ "Created by miner #" + this.minerId + "\n"
|
||||
+ "Id: " + this.id + "\n"
|
||||
+ "Timestamp: " + this.timeStamp + "\n"
|
||||
+ "Magic number: " + this.magicNumber + "\n"
|
||||
@@ -61,3 +64,4 @@ public class Block implements Serializable {
|
||||
+ "Block was generating for: " + this.generationSecs + " seconds" + "\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,10 +13,10 @@ public class BlockChain {
|
||||
this.hashZeroes = hashZeroes;
|
||||
}
|
||||
|
||||
public void addBlock() {
|
||||
public synchronized void addBlock(final int minerId) {
|
||||
final int nextId = blockList.size();
|
||||
final String previousBlockHash = (nextId > 0) ? blockList.get(nextId - 1).getBlockHash() : "0";
|
||||
blockList.add(new Block(previousBlockHash, nextId, hashZeroes));
|
||||
blockList.add(new Block(previousBlockHash, minerId, nextId, hashZeroes));
|
||||
}
|
||||
|
||||
public boolean validateBlockchain() {
|
||||
|
||||
17
src/model/Miner.java
Normal file
17
src/model/Miner.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package model;
|
||||
|
||||
public class Miner extends Thread {
|
||||
|
||||
private final int minerId;
|
||||
private final BlockChain blockChain;
|
||||
|
||||
public Miner(final int minerId, final BlockChain blockChain) {
|
||||
this.minerId = minerId;
|
||||
this.blockChain = blockChain;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
blockChain.addBlock(minerId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user