Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pomoc;
- import java.util.ArrayList;
- import java.util.concurrent.ThreadLocalRandom;
- public class App extends Thread{
- public static ArrayList<String> lista;
- String name;
- int actiontype; //podajemy 1 jak watek ma dodawac, -1 jak usuwac
- int random;
- public App(String s, int type){this.name=s; this.actiontype=type; random = ThreadLocalRandom.current().nextInt(4000, 8000 + 1);}
- public void run(){
- while(true){
- try {
- sleep(random);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- action();
- System.out.println(lista);
- }
- }
- public synchronized void action(){
- if(actiontype==1){lista.add(name);System.out.println("akcja add "+name); return;}
- if(actiontype==-1){
- if(!lista.isEmpty())
- lista.remove(0);
- System.out.println("akcja remove "+name);
- return;}
- else
- System.out.println("zly actiontype"+ name);
- }
- public static void main(String[] args) {
- App.lista=new ArrayList<String>();
- App w1=new App("Thread1",1);
- App w2=new App("Thread2",1);
- App w3=new App("Thread3",-1);
- App w4=new App("Thread4",1);
- App w5=new App("Thread5",-1);
- App w6=new App("Thread6",-1);
- w1.start();
- w2.start();
- w3.start();
- w4.start();
- w5.start();
- w6.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement