Advertisement
theSwamz

UpdatedMining

Jul 20th, 2021
1,820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.94 KB | None | 0 0
  1. package exercise5;
  2.  
  3. import exercise2.Block;
  4. import exercise4.Listener;
  5.  
  6. import java.io.UnsupportedEncodingException;
  7. import java.security.NoSuchAlgorithmException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.concurrent.BlockingQueue;
  11. import java.util.concurrent.CopyOnWriteArrayList;
  12. import java.util.concurrent.LinkedBlockingQueue;
  13.  
  14. public class ThreadMining extends Thread implements BlockListener {
  15.  
  16.     private static final List<BlockListener> listeners = new CopyOnWriteArrayList<>();
  17.     private final BlockingQueue<List<BlockChain>> queue = new LinkedBlockingQueue<>();
  18.  
  19.     //we might need this for thread termination?
  20.     private volatile boolean running = true;
  21.  
  22.     //constructor
  23.     public ThreadMining(String name){
  24.         listeners.add(this);
  25.         Thread.currentThread().setName(name);
  26.     }
  27.  
  28.     @Override
  29.     public void messageReceived(List<BlockChain> chain){
  30.         try{
  31.             queue.put(chain);
  32.         }catch (InterruptedException e){
  33.             e.printStackTrace();
  34.         }
  35.     }
  36.  
  37.     @Override
  38.     public void run(){
  39.         List<BlockChain> chain = new ArrayList<>();
  40.  
  41.         try{
  42.              while (chain.size() < 10){
  43.  
  44.                 MineAndVerify block = new MineAndVerify();
  45. //                int minerId = Integer.parseInt(Thread.currentThread().getName());
  46.                 int minerId = Character.getNumericValue(Thread.currentThread().getName().charAt(currentThread().getName().length()-1));
  47.                 System.out.println("Current miner: " +minerId);
  48.  
  49.                 BlockChain newBlock = block.mineTheNextBlock(minerId);
  50.  
  51.                 if(newBlock != null){
  52.                     chain.add(newBlock);
  53.                     sendToAllOtherThreads(chain);
  54.                 }
  55.  
  56.                 List<BlockChain> newChain = new ArrayList<>();
  57.                 newChain = queue.take();
  58.                 if(block.verifyChain(newChain) && newChain.size() > chain.size()){
  59.                     chain = newChain;
  60.                 }
  61.  
  62.                 if(chain.size() == 10)
  63.                     break;
  64.  
  65.                 System.out.println("current chain size: "+chain.size()+" current thread: "+Thread.currentThread());
  66.  
  67.             }
  68.             //print
  69.             for(int i = 0; i < 10; i++ ){
  70.                 System.out.println("Block_"+i+" : "+"{'nonce' : " + chain.get(i).getNonce() + " , 'miner': " + chain.get(i).getMiner());
  71.             }
  72.  
  73.  
  74.         }catch (UnsupportedEncodingException | NoSuchAlgorithmException | InterruptedException e) {
  75.             e.printStackTrace();
  76.         }
  77.     }
  78.  
  79.     public void sendToAllOtherThreads(List<BlockChain> message){
  80.         for(BlockListener listener: listeners){
  81.             if(listener == this){
  82.                 continue;
  83.             }
  84.             listener.messageReceived(message);
  85.         }
  86.     }
  87.     public static void main(String[] args){
  88.         System.out.println("We will create 10 threads to show mining in a decentralized manner");
  89.  
  90.         ThreadMining thread0 = new ThreadMining("Thread0");
  91.         ThreadMining thread1 = new ThreadMining("Thread1");
  92.         ThreadMining thread2 = new ThreadMining("Thread2");
  93.         ThreadMining thread3 = new ThreadMining("Thread3");
  94.         ThreadMining thread4 = new ThreadMining("Thread4");
  95.         ThreadMining thread5 = new ThreadMining("Thread5");
  96.         ThreadMining thread6 = new ThreadMining("Thread6");
  97.         ThreadMining thread7 = new ThreadMining("Thread7");
  98.         ThreadMining thread8 = new ThreadMining("Thread8");
  99.         ThreadMining thread9 = new ThreadMining("Thread9");
  100.  
  101.         thread0.start();
  102.         thread1.start();
  103.         thread2.start();
  104.         thread3.start();
  105.         thread4.start();
  106.         thread5.start();
  107.         thread6.start();
  108.         thread7.start();
  109.         thread8.start();
  110.         thread9.start();
  111.  
  112.     }
  113.  
  114.  
  115.  
  116.  
  117. }
  118. /*
  119. Notes: We need maybe a global list our threads can access...so we can keep track of our list....
  120.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement