Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package threads;
- public class Ticket extends Thread {
- private int num;
- boolean reserved;
- static int bilety[] = new int[3];
- public Ticket(int num) {
- this.num = num;
- this.reserved = false;
- }
- public Ticket(String string) {
- }
- public String toString() {
- return "Bilet [num = " + num + "]";
- }
- public void run() {
- int i = 0;
- for(int j = 0; j < 6; j++)
- {
- while (!reserved)
- {
- for(i = 0; i < 3; i++)
- {
- if(bilety[i] == 0)
- {
- bilety[i] = 1;
- reserved = true;
- System.out.println("Wątek nr." + num + " zarezerwował bilet nr." + (i+1));
- break;
- }
- }
- try {
- sleep(1000);
- } catch (InterruptedException e) {
- System.out.println("Przerwano wątek nr." + num);
- return;
- }
- }
- if(j > 0)
- {
- bilety[i] = 0;
- System.out.println("Wątek nr." + num + " zwolnił rezerwację biletu nr." + (i+1));
- break;
- }
- try {
- sleep(1000);
- } catch (InterruptedException e) {
- System.out.println("Przerwano wątek nr:" + num);
- return;
- }
- }
- }
- public static void main(String[] args) {
- Ticket b[] = new Ticket[10];
- for(int k = 0; k < 10; k++)
- {
- b[k] = new Ticket(k+1);
- //System.out.println(b[k]);
- b[k].start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement