Saturday, September 11, 2010

ජාවා යොදාගෙන බහුකාර්‍ය වැඩසටහන් ලිවීම (Java Threads) II

අපි මීට පෙර පාඩමේදී සාකච්ඡා කලා Threads හා සම්බන්ද මූලිකම මූලධර්ම පිලිබඳව. අපි මෙම පාඩමෙන් බලමු Threads යොදාගෙන වැඩසටහන් ලියන ආකාරය.
Threads යොදාගෙන ජාවා වැඩසටහන් ලිවීමේදී ඉතා වැදගත් වන methods කිහිපයක් තිබෙනවා අපි මුලින්ම එම methds පිලිබඳ සලකා බලමු.
ඕනෑම Thread එකක් සැලකීමේදී එය පවත්නා තත්වයන් කිහිපයක් ඇත. පහත Thread state diagram එක මගින් එම අවස්ථා පිලිබඳ සහ thread එකක හැසිරීම පිලිබඳ අධ්‍යයනය කරමු.
 Fig 1.0 Thread State Diagram
 
Method Name Description
Thread.sleep()  දෙන ලද කාලයක් සඳහා අවශ්‍ය thread එකක් නවතා දැමිය හැක (pause).
syntax:
sleep(long milliseconds)
sleep(long ms,long nanosec)
eg:
try{
    Thread.sleep(1000);
}catch(InterruptedException e){
    System.err.println("Operation was Interrupted!");
}

Thread.yield() අනෙකුත් threads සඳහා execute වීමට ඉඩ ලබාදේ. Thread synchronization පිළිබඳ සාකච්ඡා කිරීමේදී මේ ගැන වැඩිදුරටත් අධ්‍යයනය කරමු.
Thread.start() අදාල Thread එක ආරම්භ කෙරේ. එනම් thread එක runnable state එකට පත්කරයි.
Thread.isAlive()  දෙන ලද thread එකක් ජීවී තත්වයේ පවතීදැයි පරීක්ශා කෙරේ. Thread එක ජීවී තත්වයේ පවතී නම් true ලෙසටද නැතහොත් false ලෙසටද return කෙරේ. Thread එකක් live තත්වයේ පැවතීමට නම් එය start වී තිබීම හා එය dead state එකේ නොතිබීම අනිවාර්‍ය වේ.
Thread.activeCount() Active threads ප්‍රමාණය return කරයි.
Thread.enumurate() මේ වන විට active තත්වයේ පවතින සියලු threads දෙන ලද array එකකට fill කිරීම සිදුකරයි.
Thread.currentThread()  Current thread එක return කරයි.
Thread.dumpStack()  ජාවා වැඩසටහන් debug කිරීමේදී වැදගත් වේ.
Thread.setPriority()  කිසියම් Thread එකකට ලබාදෙන priority එක int value එකක් ලෙස ලබාදේ. (1 සිට 10 දක්වා)
Thread.getId()  දෙනලද thread එකක් හඳුනාගැනීම සඳහා ලැබී ඇති ID එක long value එකක් ලෙස return කරයි.
Thread.setDaemon()  කිසියම් thread එකක් daemon ස්වභාවයට පත් කරයි. Daemon thread එකක් යනු user defined threads සඳහා සහය දැක්වීම සඳහා පසුබිමින් ධාවනය වන(run) threads වේ. JVM එකේ Garbage collector එක මීට උදාහරණයකි.

මෙම method එක මගින් අප විසින් ලියන ලද කිසියම් thread එකක් daemon nature එකට පත්කල හැක. සාමාන්‍යයෙන් daemon thread එකකට ලැබෙනුයේ low priority එකකි. නමුත් අපට අවශ්‍ය නම් daemon threads සඳහා වැඩි priority එකක් ලබාදිය හැක.

මෙහිදී විශේෂයෙන් සැලකිය යුතු කාරණය වන්නේ ඕනෑම daemon thred එකක් ආරම්භ වීමෙන් පසු එහි daemon ස්වභාවය වෙනස් කල නොහැකිය යන්නයි. නමුත් එහි priority එක වෙනස් කල හැක.

eg:
MyThread mt=new MyThread();
mt.setDaemon(true);
mt.start();
Thread.run()  Thread එකක වැදගත්ම method එකක් ලෙස සැලකිය හැක. ඕනෑම thread එකක මෙම method එක override කර තිබිය යුතුය. start() method එක call කිරීමෙන් පසු execute කිරීමට පටන්ගන්නේ මෙම run() method එක තුල ඇති විධානයන් වේ.
Thread.destroy()  කිසිඳු cleanup එකකින් තොරව දෙනලද thread එකක් destroy කරදමයි. මෙම method එක deadlocks ඇතිවීමට හේතුවන නිසා මෙය deprecated method එකක් ලෙස දක්වා ඇත. Deadlocks පිලිබඳ ඉදිරි පාඩමකදී සාකච්ඡා කෙරේ.
Thread.stop()  කිසියම් thread එකක් stop කිරීමට යොදාගැනේ. මෙයද unsafe operation එකක් ලෙස සැලකේ. එම නිසා මෙය depricated method එකක් ලෙස දක්වා ඇත. Thread එකක් නැවැත්වීම සඳහා stop method එක භාවිතා නොකර වෙනත් technique එකක් භාවිතා කර ආරක්ශිත ආකාරයකට threads නවතා දමන අයුරු අපි ඉදිරියේදී සාකච්ඡා කරමු.

ජාවා භාවිතයෙන් thread create කරන අයුරු ආදර්ශනය කිරීමට ලියන ලද පහත සරල වැඩසටහන හොඳින් අධ්‍යයනය කරන්න.

ThreadLauncher.java
/**     
*class  : ThreadLauncher     
*Author : Kanishka Dilshan     
*Purpose: Showing how to create threads in Java 
*Blog   : http://javaxclass.blogspot.com     
*/ 

class ThreadLauncher{
 public static void main(String args[]){
  MyThread mt1=new MyThread("motor1");
  MyThread mt2=new MyThread("motor2");
  mt1.start();
  mt2.start();
 }
}

class MyThread extends Thread{
 String name;
 public MyThread(String name){
  this.name=name;
 }
 
 public void run(){
  System.out.println("Thread : "+name+" started!");
  for(int i=0;i<5;i++){
   System.out.println("I am "+name+ " Thread.");
   try{
    this.sleep(1000);
   }catch(InterruptedException e){
    System.err.println("Thread was interrupted! "+ e.getMessage());
   }
  }
  System.out.println("Thread "+name+" is going to die!");
 }
}
Code 1.0
Output:
Fig 2.0 Output of code 1.0
Threaded program එකක out put එක ඉහත පරිදි වේ.  mt1 හා mt2 යනු threads 2යි. අපි එම වැඩසටහනම mt1 හා mt2 යනු runnable නොවන objects ලෙස සකස් කලහොත් output එක පහත පරිදි වේ.
ඔබට threads පිලිබඳ යම් අවබෝධයක් දැන් ලැබී තිබිය යුතුය. අපි ඉදි පාඩම් වලින් තවදුරටත් threads පිලිබඳ සාකච්ඡා කරමු.

4 comments:

  1. එල එල දිගටම ලියන්න

    ReplyDelete
  2. fattaa digatama liyanna

    ReplyDelete
  3. පිං සිද්ද වෙනවා මේ පෝස්ට් එක ලිව්වට...

    ReplyDelete
  4. නියමෙට විස්තර කරලා තියෙනවා.............

    ReplyDelete