读者 - 作家和优先权

时间:2016-07-30 14:25:11

标签: java multithreading

我正在使用带有Java线程的系统Reader-Writer,并且必须优先考虑:读者优先于作者。

我写了一个源代码,编译并且可以毫无问题地执行。但我想确定它是正确的。

你能否告诉我你是否看到了一些错误?

嗯,首先我必须向你解释我的小程序的目的。每隔一段时间,会向用户显示一条消息。后者可以修改它,并改变其显示延迟(“时间间隔”)。消息由ID标识。

因此,如果用户输入:0 \n Hello \n 2,则表示消息n°0现在为“Hello”,并且每2秒显示一次。

每条消息由一个线程处理。 我必须使用信号量。

SOURCE-CODES。

读者:

public class Lecteur extends Thread {

    private Message<String> message;

    public Lecteur(Message<String> message) {
        this.message = message;
    }

    public void run() {
        try {
            while(true) {
                System.out.println(message.getContent());
                int time = message.getRefresh_time()*1000;
                Thread.sleep(time);
            }
        } catch(InterruptedException e) {
            System.out.println(e);
        }
    }

}

作者:

import java.util.HashMap;
import java.util.Scanner;

public class GestionnaireSaisie extends Thread {

    private HashMap<Integer, Message<String>> messages;

    public GestionnaireSaisie(HashMap<Integer, Message<String>> messages) {
        this.messages = messages;
    }

    public void run() {
        Scanner scanner = new Scanner(System.in);

        int id;
        String content;
        int time_refresh;

        while (true) {
            id = scanner.nextInt();
            content = scanner.next();
            time_refresh = scanner.nextInt();

            Message<String> found_msg = messages.get(id);
            found_msg.setContent(content);
            found_msg.setRefreshTime(time_refresh);
        }
    }

}

最有趣的类,包含共享数据的共享对象:

import java.util.concurrent.Semaphore;

public class Message<T> {

    private static int maxid;
    private int id;
    private T content;
    private int refresh_time;

    public Semaphore mutex_content, mutex_refresh_time, semNbl;
    public static int nbL = 0;

    public int getId() {
        return id;
    }

    public Message(T content, int refresh_time, Semaphore mutex_content, Semaphore mutex_refresh_time, Semaphore semNbl) {
        id = maxid;
        Message.maxid++;
        this.content = content;
        this.refresh_time = refresh_time;

        this.mutex_content = mutex_content;
        this.mutex_refresh_time = mutex_refresh_time;
        this.semNbl = semNbl;
    }

    // <!-- CONTENT
    public void setContent(T content) {
        try {
            mutex_content.acquire();
            this.content = content;
            mutex_content.release();
        } catch(InterruptedException e) {
            System.out.println(e);
        }
    }

    public T getContent() {
        T ret = null;
        try {
            semNbl.acquire();
            Message.nbL++;
            if(Message.nbL == 1) {
                mutex_content.acquire();
            }
            semNbl.release();

            ret = content;

            semNbl.acquire();
            Message.nbL--;
            if(Message.nbL == 0) {
                mutex_content.release();
            }
            semNbl.release();

        } catch(InterruptedException e) {
            System.out.println(e);
        }

        return ret;
    }
    // CONTENT -->

    // <!-- REFRESH TIME
    public void setRefreshTime(int refresh_time) {
        try {
            mutex_refresh_time.acquire();
            this.refresh_time = refresh_time;
            mutex_refresh_time.release();
        } catch(InterruptedException e) {
            System.out.println(e);
        }
    }

    public int getRefresh_time() {
        int ret = 0;

        try {
            semNbl.acquire();
            Message.nbL++;
            if(Message.nbL == 1) {
                mutex_refresh_time.acquire();
            }
            semNbl.release();

            ret = refresh_time;

            semNbl.acquire();
            Message.nbL--;
            if(Message.nbL == 0) {
                mutex_refresh_time.release();
            }
            semNbl.release();

        } catch(InterruptedException e) {
            System.out.println(e);
        }
        return ret;
    }
    // REFRESH TIME -->
}

这里有一些代码来测试它:

Semaphore mutex_content = new Semaphore(1);
Semaphore mutex_refresh_time = new Semaphore(1);
Semaphore semNbl = new Semaphore(1);

Message<String> message1 = new Message<String>("Bonjour le monde !", 5, mutex_content, mutex_refresh_time, semNbl);
new Lecteur(message1).start();

HashMap<Integer, Message<String>> messages = new HashMap<Integer, Message<String>>();
messages.put(message1.getId(), message1);
GestionnaireSaisie gs = new GestionnaireSaisie(messages);
gs.start();

0 个答案:

没有答案