Thread

Màu nền
Font chữ
Font size
Chiều cao dòng

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package gaj_lab2;

/**

 *

 * @author dong

 */

public class Common {

   

    // Check is Oll number

    public static boolean isSoLe(int number) {

        if (number % 2 == 0) {

            return false;

        }

        return true;

    }

    // Check is Nguyen To number

    public static boolean isSoNguyenTo(int number) {

        for (int i=2; i<=number/2; i++) {

            if (number % i == 0) {

                return false;

            }

        }

        return true;

    }

    // Check so HOAN HAO

    public static boolean isSoHoanHao(int number) {

        int sum=0;

        for (int i=1; i<=number/2; i++) {

            if (number % i == 0) {

                sum += i;

            }

        }

        if (sum != number) {

            return false;

        }

        return true;

    }

    // Check so CHINH PHUONG

    public static boolean isSoChinhPhuong(int number) {

        double so_du = Math.sqrt(number);

        double so_nguyen = Math.round(so_du);

        if (so_du != so_nguyen) {

            return false;

        }

        return true;

    }

   

}

 

\\ClassComsumer

class Consumer implements Runnable {

    Queue q;

    Consumer (Queue q) {

        this.q = q;

        new Thread (this, "Consumer").start();

    }

    public void run() {

        for (int i = 2; i <= 100; i++) {

            q.get();

        }

    }

}

 

\\Class Pubicher

class Publisher implements Runnable {

    Queue q;

    Publisher(Queue q) {

        this.q = q;

        new Thread (this, "Publisher").start();

    }

    public void run() {

        for (int i = 2; i <= 100; i++) {

            q.put(i);

        }

    }

}

 

\\Functin cua Theare

public class Queue {

    int exchangeValue;

    boolean busy = false;

   

    synchronized int get() {

        if (busy) {

            try {

                busy = false;

                wait();

            } catch (InterruptedException e) {

                System.out.println("Get: InterruptedException");

            }

        }

        String result = "";

        if (Common.isSoLe(exchangeValue)) {

            result += "Lẻ" + ",";

        } else {

            result += "Chẵn" + ",";

        }

        if (Common.isSoChinhPhuong(exchangeValue)) {

            result += "Chính PhÆ°Æ¡ng" + ",";

        }

        if (Common.isSoNguyenTo(exchangeValue)) {

            result += "Nguyên Tố" + ",";

        }

        if (Common.isSoHoanHao(exchangeValue)) {

            result += "Hoàn Hảo" + ",";

        }

        result = result.substring(0, result.length()-1);

        System.out.println(result);

        notify();

        busy = true;

        return exchangeValue;

    }

    synchronized void put (int exchangeValue) {

        if (busy) {

            try {

                busy = false;

                wait();

            } catch (InterruptedException e) {

                System.out.println("Put: InterruptedException");

            }

        }

        this.exchangeValue = exchangeValue;

        System.out.println("-------------------");

        System.out.print(exchangeValue+":");

        notify();

        busy = true;

    }

}

 

\\test

class Demo {

    public static void main(String args []) {

        Queue q = new Queue ();

        new Publisher(q);

        new Consumer(q);

    }

}

Bạn đang đọc truyện trên: Truyen2U.Pro