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

[مخالف - عنوان غير مناسب :]بعض المثود الغير مفهومة

مغلق
بدأه programing*gril في 9 مارس 2011 · 2 رد · 395 مشاهدة · في JavaEE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

هناك بعض الفنكشن التي لم افهم ما مبدأ عملها

هي مثود الـ run

public class ArrayWriter implements Runnable{

   public final SimpleArray SharedSimpleArray;
   private final int StartValue;

    public ArrayWriter(int Value, SimpleArray array){
       StartValue = Value;
       SharedSimpleArray = array;
       }// end constructor//With constructor we need to do ovarraid for run methodv

    public void run(){
        for(int i = StartValue; i <StartValue+3; i++ )
        {
            SharedSimpleArray.add(i);// add an element to the shared array

        }//end for
    }//end run
}// end class ArrayWriter

هذه كلاس اخرى قد بني منها الاوبجكت

public class SimpleArray { // not thread Safe

    private final int array[]; // the shared int array
    private int WritIndex = 0;
    private final Random generator = new Random();

    public SimpleArray (int size){
        array = new int [size];
    }// end constructor

    public void add (int value)
    {
        int position = WritIndex;
        try{// put thread to sleep
            Thread.sleep(generator.nextInt(5000));
        }// end try
          catch (InterruptedException ex){
            System.out.println("Interrupted");
        }//end catch

        array [position] = value;
        System.out.printf
                ("%s wrote %2d to element %d \n ", Thread.currentThread().getName(),value,position);
        // name of pool thread , name of thread in the pool
        // %2d locatin of thread (1,2,3 11,12, 13)
        ++ WritIndex; //(increas position)

        System.out.printf("Next writeindex : %d\n",WritIndex);
    }// end add

    public String toString(){

    String arrayString = "\n contents of simple array:\n";
    for (int i = 0 ; i < array.length ; i++)
    arrayString += array+ " " ;
    return arrayString;
    }// end to string

}// end class

وهذه المين كلاس

public class SharedArrayTest {
     public static void main (String []arg){
         //construct the shared object
         SimpleArray SharedSimpleArray = new SimpleArray(6);
         ArrayWriter Writer1 = new ArrayWriter(1, SharedSimpleArray);
         ArrayWriter Writer2 = new ArrayWriter(11,SharedSimpleArray);

         ExecutorService executer =  Executors.newCachedThreadPool();

         executer.execute(Writer1);
         executer.execute(Writer2);
         executer.shutdown();

         try{// wait l minute for booth writes to finish executing
             boolean tasksEnded = executer.awaitTermination(1, TimeUnit.MINUTES);
             // blocks until all tasks have completed execution after shutdown or time out occurs
             // or the current thread is interrupted

             if (tasksEnded)
                 System.out.println(SharedSimpleArray);
             else
                 System.out.println("Time out for tasks to finish");
         }//end try
         catch(InterruptedException e){
             System.out.println("Interruption");
         }// end catch
    }//end main
}// end class

تم تعديل هذه المشاركة بواسطة programing*gril في 9 مارس 2011 في 21:39

308219358.gif
#2

Run : هي الدالة التي يتم فيها التشغيل الفعلي لدوال البرنامج .. في المثال الذي ذكرتي ، إضافة القيم للمصفوفة يتم فعليا في الدالة run

لم افهم بقية السؤال..

تحية عطرة

عفواً أحبتي انقطاعي بسبب الدراسة دعواتكم بالتوفيق..

{ لا ينال العلم مستحٍ ولا مستكبر }

#3

الأخ الكريم/الأخت الكريمة

السلام عليكم ورحمة الله وبركاته

مرحباً بكم في منتدى الفريق العربي للبرمجة

تأسف إدارة المنتدى لغلق الموضوع وذلك لمخالفته قوانين المشاركات. فقرة اختيار عنوان مناسب لمحتوى الموضوع.

قواعد طرح المشاركات

/index.php?showtopic=29343

شاكرين لكم حُسن تعاونكم

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

هذا الموضوع مغلق.

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

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…