Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prod_cons;
- public class PC
- {
- public static void main(String[] args)
- throws InterruptedException
- {
- final Func func = new Func();
- //Producent
- Thread t1 = new Thread(new Runnable()
- {
- @Override
- public void run()
- {
- try
- {
- func.produce();
- }
- catch(InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- });
- //Konsumer
- Thread t2 = new Thread(new Runnable()
- {
- @Override
- public void run()
- {
- try
- {
- func.consume();
- }
- catch(InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- });
- t1.start();
- t2.start();
- }
- public static class Func
- {
- int capacity = 1;
- int value = 0;
- int PosA = 0, PosB = 0, PosC = 0, PosD = 0;
- char[] Passw = {'X','X','X','X'};
- boolean EndFlag = false;
- public void produce() throws InterruptedException
- {
- while (true)
- {
- synchronized (this)
- {
- if(value != capacity)
- {
- if(EndFlag) return;
- CreatePass();
- System.out.println("Password produced: ");
- System.out.println(Passw);
- value++;
- notifyAll();
- }
- if(value == capacity)
- {
- notifyAll();
- wait();
- }
- Thread.sleep(500);
- }
- }
- }
- public void consume() throws InterruptedException
- {
- char[] RandPass = {'A', 'A', 'A', 'C'};
- while (true)
- {
- synchronized (this)
- {
- while (value==0)
- {
- notifyAll();
- wait();
- }
- value--;
- if(Passw[0] == RandPass[0] && Passw[1] == RandPass[1] && Passw[2] == RandPass[2] && Passw[3] == RandPass[3])
- {
- System.out.println("Passwords Match!");
- EndFlag = true;
- notifyAll();
- return;
- }
- else
- {
- System.out.println("No match!");
- }
- Thread.sleep(200);
- }
- }
- }
- void CreatePass()
- {
- char charA = 0, charB=0,charC=0,charD=0;
- switch(PosA)
- {
- case(0):
- charA = 'A';
- break;
- case(1):
- charA = 'B';
- break;
- case(2):
- charA = 'C';
- break;
- case(3):
- charA = 'D';
- break;
- default:
- System.out.println("WTF-A");
- }
- switch(PosB)
- {
- case(0):
- charB = 'A';
- break;
- case(1):
- charB = 'B';
- break;
- case(2):
- charB = 'C';
- break;
- case(3):
- charB = 'D';
- break;
- default:
- System.out.println("WTF-B");
- }
- switch(PosC)
- {
- case(0):
- charC = 'A';
- break;
- case(1):
- charC = 'B';
- break;
- case(2):
- charC = 'C';
- break;
- case(3):
- charC = 'D';
- break;
- default:
- System.out.println("WTF-C");
- }
- switch(PosD)
- {
- case(0):
- charD = 'A';
- break;
- case(1):
- charD = 'B';
- break;
- case(2):
- charD = 'C';
- break;
- case(3):
- charD = 'D';
- break;
- default:
- System.out.println("WTF-D");
- }
- Passw[0] = charA;
- Passw[1] = charB;
- Passw[2] = charC;
- Passw[3] = charD;
- if(PosD == 3)
- {
- PosD = 0;
- if(PosC == 3)
- {
- PosC = 0;
- if(PosB == 3)
- {
- PosB =0;
- if(PosA == 3)
- {
- System.out.println("All passwords exhausted");
- }
- else
- {
- PosA++;
- }
- }
- else
- {
- PosB++;
- }
- }
- else
- {
- PosC++;
- }
- }
- else {PosD++;}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement