Callable, nie wykonuje pętli while(true)

0

Chodzi o to że wątki miały nieprzerwanie dokonywać tarnsakcji, czyli zabierać talerz, i odnosić,
Jest ustawiona pętla while, a pomimo tego, program się wykonuje tylko jakiś czas i potem się kończy. W kodzie jest zanaczona linijka.
I czy porpawnie jest synchronizowana?

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;

public class MainClass {

		public static void main(String[] args) throws InterruptedException, ExecutionException {
	
			Bank bank=new Bank(10, 10);
			List<Future<ThreadInfo>> list = new ArrayList<>();
			ExecutorService pool=Executors.newFixedThreadPool(2);
			for(int i =0; i< 50; ++i) {
				list.add(pool.submit(new User(i, bank)));
			}
//			list.add(pool.submit(new User(0, bank)));
//			list.add(pool.submit(new User(1, bank)));
//			list.add(pool.submit(new User(2, bank)));
			
			pool.shutdown();
			for(Future<ThreadInfo> t: list) {
				System.out.println("Wątek "+ t.get().getName());	
			}
	
	}
}
	class ThreadInfo{
		private String name;
		
		public ThreadInfo(String name) {
			this.name=name;
		}
	
		public String getName() {
			return this.name;
		}
	}
	
	class User implements Callable<ThreadInfo>{
		private int kto;
		private Bank bank;
		public User(int kto,Bank bank) {
			this.kto=kto;
			this.bank=bank;
		}
		@Override
		public ThreadInfo call(){
			Random r=new Random();
			while(true) {   ###################### TUTAJ
				try {
					int czynnosc = r.nextBoolean()? 1:0;
					bank.transfer(czynnosc, kto);
					Thread.currentThread().sleep(100);
				} 
				
						
				catch (InterruptedException e) {
					e.printStackTrace();
				}
				return new ThreadInfo("yes");
			}
		}
	
	}
	


class Bank {
	private int[] talerze = new int[2];

	public Bank() {
		this.talerze[0] = 10;
		this.talerze[1] = 10;
	}
	public Bank(int czyste, int brudne) {
		this.talerze[0] = czyste;
		this.talerze[1] = brudne;
	}

	public synchronized void transfer(int czynnosc, int kto) throws InterruptedException {
		long startTime=System.currentTimeMillis();
		while(this.talerze[0] + this.talerze[1] < 10) {
			wait();
		}
		
		if(czynnosc == 1) {
			this.talerze[1] += 1;
			this.talerze[0] -= 1;
		}
		else {
			this.talerze[0] += 1;
			this.talerze[1] -= 1;
		}
	
		String wykonal = czynnosc == 1? "ubrudzil": "wyczyscil";
		System.out.println("Wątek "+Thread.currentThread().getName()+" kelner " + wykonal +" talerz");
		System.out.println("Talerzy: "+ getBalance());
		notifyAll();
		long endTime=System.currentTimeMillis();
		System.out.println(">>"+(int)(endTime-startTime));
	}
	
	public synchronized int getBalance() {
		return this.talerze[0]+ this.talerze[1];
	}
}

0

Na pewno problemem jest to, że robiąc return w pętli while ta pętla wykonuje się raz. Czy reszta ma sens? - nie wiem, nie analizowałem, zerknąłem tylko na te kilka linijek.

0
Swr napisał(a):

Na pewno problemem jest to, że robiąc return w pętli while ta pętla wykonuje się raz. Czy reszta ma sens? - nie wiem, nie analizowałem, zerknąłem tylko na te kilka linijek.

za bardzo sobie mózg zakodziłem... no rozumiem...

1 użytkowników online, w tym zalogowanych: 0, gości: 1