Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package exercise5;
- import exercise2.Block;
- import exercise4.Listener;
- import java.io.UnsupportedEncodingException;
- import java.security.NoSuchAlgorithmException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.CopyOnWriteArrayList;
- import java.util.concurrent.LinkedBlockingQueue;
- public class ThreadMining extends Thread implements BlockListener {
- private static final List<BlockListener> listeners = new CopyOnWriteArrayList<>();
- private final BlockingQueue<List<BlockChain>> queue = new LinkedBlockingQueue<>();
- //we might need this for thread termination?
- private volatile boolean running = true;
- //constructor
- public ThreadMining(String name){
- listeners.add(this);
- Thread.currentThread().setName(name);
- }
- @Override
- public void messageReceived(List<BlockChain> chain){
- try{
- queue.put(chain);
- }catch (InterruptedException e){
- e.printStackTrace();
- }
- }
- @Override
- public void run(){
- List<BlockChain> chain = new ArrayList<>();
- try{
- while (chain.size() < 10){
- MineAndVerify block = new MineAndVerify();
- // int minerId = Integer.parseInt(Thread.currentThread().getName());
- int minerId = Character.getNumericValue(Thread.currentThread().getName().charAt(currentThread().getName().length()-1));
- System.out.println("Current miner: " +minerId);
- BlockChain newBlock = block.mineTheNextBlock(minerId);
- if(newBlock != null){
- chain.add(newBlock);
- sendToAllOtherThreads(chain);
- }
- List<BlockChain> newChain = new ArrayList<>();
- newChain = queue.take();
- if(block.verifyChain(newChain) && newChain.size() > chain.size()){
- chain = newChain;
- }
- if(chain.size() == 10)
- break;
- System.out.println("current chain size: "+chain.size()+" current thread: "+Thread.currentThread());
- }
- //print
- for(int i = 0; i < 10; i++ ){
- System.out.println("Block_"+i+" : "+"{'nonce' : " + chain.get(i).getNonce() + " , 'miner': " + chain.get(i).getMiner());
- }
- }catch (UnsupportedEncodingException | NoSuchAlgorithmException | InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void sendToAllOtherThreads(List<BlockChain> message){
- for(BlockListener listener: listeners){
- if(listener == this){
- continue;
- }
- listener.messageReceived(message);
- }
- }
- public static void main(String[] args){
- System.out.println("We will create 10 threads to show mining in a decentralized manner");
- ThreadMining thread0 = new ThreadMining("Thread0");
- ThreadMining thread1 = new ThreadMining("Thread1");
- ThreadMining thread2 = new ThreadMining("Thread2");
- ThreadMining thread3 = new ThreadMining("Thread3");
- ThreadMining thread4 = new ThreadMining("Thread4");
- ThreadMining thread5 = new ThreadMining("Thread5");
- ThreadMining thread6 = new ThreadMining("Thread6");
- ThreadMining thread7 = new ThreadMining("Thread7");
- ThreadMining thread8 = new ThreadMining("Thread8");
- ThreadMining thread9 = new ThreadMining("Thread9");
- thread0.start();
- thread1.start();
- thread2.start();
- thread3.start();
- thread4.start();
- thread5.start();
- thread6.start();
- thread7.start();
- thread8.start();
- thread9.start();
- }
- }
- /*
- Notes: We need maybe a global list our threads can access...so we can keep track of our list....
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement