الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

خوارزمية الاقصاء المتبادل باستخدام الthread

بدأه Bassam Badr في 11 فبراير 2013 · 2 رد · 557 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم 

سعيد جداً بالانضمام الى هذا الموقع الرائع واتمنى ان اجد منكم من يساعدنا 

turn = 0 // shared control variable    

while (turn != i);

// CS

turn = (turn + 1) % n;

 

`هذه الخوارزمية مطلوب مني اقوم بتحويلها الى thread باستخدام الجافا وحاولت كثيراً لكني لم افلح توصلت في الاخير الى هذا الكود لكنه مليء بالغباء للاسف الشديد

 


/*
 * 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);
    }
}

 

هل يوجد احد يستطيع ان يساعدنا ويقلي ايش هي اخطائي وكيف اقدر اتخلص من هذه الاخطاء وشكراً

#2

كيف قمت باستخدام obj في الداله run وهو غير معرف فيها؟ وغير معرف في الفئه ؟ انت قمت بارساله الى الــconstructor وليس الى الداله run

1

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#3

نعم اخي لقد انتبهت لهذا الخطأ البارحه وقمت بحل المشكله  شكراً اخي

 

/*
 * Mutual exclusion using thread
 */

class newThread extends Thread{

    static int i=0;
    final int n=10;
        
    newThread(){
	start();
    }

    public void run(){
	if(i==n)
            i=0;

	criticalSection();
        new newThread();
	
    }

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

class aa{
    public static void main(String args[]){
	
	new newThread();
        
    }
}

مواضيع مشابهة