互斥码 [英] Mutual exclusion code

查看:89
本文介绍了互斥码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此代码转换为java并使用线程来实现它

I'm trying to convert this code to java and using thread to implement it

turn = 0 // shared control variable    
while (turn != i);
// CS
turn = (turn + 1) % n;  

我真的很努力地找到正确的代码,但是我失败了,这是我的代码

I'm really tried hard to reach to right code but I failed this is my code

/*
 * Mutual exclusion using thread
 */
class gV{
int turn=0;
}

class newThread extends Thread{

    static int i;
    int n=10;
    newThread(gV obj){
    this.i=obj.turn;
    start();
    }

    public void run(){

    while(obj.turn!=i&&obj.turn<n);
    criticalSection(i);
    obj.turn=(obj.turn+1);
    i++;
    }

    public void criticalSection(int numOfProcess){
    System.out.println("Process " + numOfProcess + " done!!");
    }
}

class MutualExclusion{
    public static void main(String args[]){
    gV obj = new gV();
    new newThread(obj);
    }
}  

我知道我的代码有一些错误.谢谢您的帮助!

I know my code has some mistakes. Thank you for the help!

推荐答案

使用AtomicInteger.

Atomic意味着对它的任何操作将在其他线程看不到结果之前完全完成.这意味着您将不会同时执行两个操作来夸张"它.例如,假设您有一个非原子整数,并且有两个线程试图同时对其进行递增操作(例如,其值为1),它们都将其读取为1,然后尝试将其设置为2.它们都对其进行了一次递增处理,但是没有使用它变成3,变成2! AtomicInteger通过为您提供IncrementAndGet来解决此问题,该保证可以确保没有其他线程可以在增量完成之前访问AtomicInteger的值.

Atomic means that any operation on it will fully complete before any other thread can see the result. Meaning that you won't have two simultaneous operations 'clobber' it. For example, imagine if you had a non atomic integer and two threads attempted to increment it simultaneously - say it had value 1, they both read it as 1 and attempt to set it to 2. They both incremented it once - but instead of it becoming 3, it became 2! AtomicInteger solves this problem by giving you IncrementAndGet, which guarantees no other thread can access the AtomicInteger's value before the increment completes.

尤其要使用以下方法:

http: //docs.oracle.com/javase/6/docs/api/java/util/concurrent/atomic/AtomicInteger.html#get()

http: //docs.oracle.com/javase/6/docs/api/java/util/concurrent/atomic/AtomicInteger.html#incrementAndGet()

您可能会注意到这会增加它,但不会以n为模.好吧,只要您读取它的值,就可以取n为模,而无需以这种方式存储它.

You might notice that this increments it, but it doesn't take it modulo n. Well, you can take it modulo n whenever you read its value, you don't need it to be stored that way.

顺便说一句,做这样的事情:

By the way, doing something like this:

while (turn != i);

被称为繁忙等待,这是一个坏主意,因为这意味着CPU使用率将为100%,每秒检查该变量数十万次.在这种情况下,您不想让每个线程都尽可能频繁地进行检查,而是想让线程wait并在轮到该线程继续执行时被另一个线程notify对待.

is called busy-waiting, and it's a bad idea because it means that CPU usage will be 100%, checking the variable hundreds of thousands of times per second. In this kind of scenario, instead of making each thread check as often as possible, you want to have threads wait and be notifyed by another thread when it is that thread's turn to continue execution.

我相信在Java中,使用locksynchronized来实现互斥也会为您提供此属性,例如如果尝试锁定某物或输入一个同步块,但该块已在使用中,则该线程进入睡眠状态,并在轮到该线程时被唤醒.因此,您也可以对此进行调查.

I believe in Java that using lock and synchronized to implement mutual exclusion will also give you this property, e.g. if you try to lock on something or enter a synchronized block but it is already in use then the thread goes to sleep and is woken up when it is its turn. So, you can look into this as well.

这篇关于互斥码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆