~~ Java උදවුවක් ඕනේ ~~

MihiCherub

Well-known member
  • Sep 14, 2009
    18,919
    1
    9,716
    113
    Gampaha
    දන්න අය ඉන්නව නම් කියන්ඩෝ..

    මේකයි වැඩේ Method 2ක් තියෙනවා. Method 2කම ඇතුලෙ එකිනෙකට වෙනස් Thread 2ක් Runnable එකක් තුල Run වෙනවා.


    Code:
    void m1(){
        t1 = new Thread(new Runnable() {
        
            @Override
            public void run() {
    
                Thread.sleep(1000);
                //My Codes
            }    
    
        });
            t1.start();
    }
    
    void m2(){
        t2 = new Thread(new Runnable() {
        
            @Override
            public void run() {
    
                Thread.sleep(1000);
                //My Codes
    
            }    
    
        });
            t2.start();
    }

    මම වෙන Method එකක if-else condition එකක ඉදන් මේ Thread Method එකට Call කරනවා

    Code:
    void call(){
        while(true){
            if(a==1){
                m1("A");
                m2("B");
            }else if(a==1){
                m1("C");
                m2("D");
            }else if(a==1){
                m1("E");
                m2("F");
            }else{
                m1("G");
                m2("H");
            }
        }
    }

    ඔය උඩ තියන විදියට. ඒත් ම්ට ඕනෙ if-else Condition එක තුල a==1 or 2 likewise වෙනවිට m1 හා m2 method දෙකම එකවර වෙන්න ඕනෙ. ඊට පස්සෙ while loop එක true නිසා මේක continue සිදු වෙනවා. ඒත් m1 හා m2 method වල thread එක runවෙනවිට ආයිත් m1 and m2 call වුනොත් run වෙන thread එකට බාධා නොකර holdවෙන්න ඕනෙ. q එකක තියෙන්න ඕනෙ. නමුත් දැන් තියෙන code එකේ අච්චාරුවක් වගේ run වෙනව. මට මේක හරියට synchronized කරල දෙන්නකෝ..
     

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,919
    1
    9,716
    113
    Gampaha
    Code:
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    /**
     *
     * @author MihiCherub
     */
    public class test {
       
        private static void m1(final String a) {
            Thread t1 = new Thread(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        System.out.print(a);
                    } catch (InterruptedException ex) {
                      //  Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
            t1.start();
        }
    
        private static void m2(final String a) {
            Thread t2 = new Thread(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        System.out.print( " "+a);
                    } catch (InterruptedException ex) {
                       // Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
            t2.start();
        }
    
        public static void main(String[] args) {
            for (int i = 0; i < 10; i++) {
                m1("Mihi");
                m2("Cherub");
                System.out.println("");
            }
        }
    }

    කවුරුත් නැද්දෝ.. මේක තමා Code එක. මට ඕනෙ මෙකෙ MihiCherub කියල 10 පාරක් output එක විදියට ගන්න. ඒත් එන්නෙ අච්චාරුවක් වගේ.

    MihiCherub
    MihiCherub

    ඔන්න ඔය ව්දියට ඒත් එන්නෙ.

    MihiMihi Cherub CherubMihi CherubMihiMih කියල.. Sync කරල දෙන්නකෝ
     

    yrollgayanth

    Member
    Aug 26, 2008
    1,524
    113
    0
    Melbourne, AU
    Umbata mehemada one

    Code:
    package testthread;
    
    
    /*
     * @author Yrol Fernando
     */
    
    class TestSync implements Runnable{
        
        private int count = 0;
        private String word;
    
        @Override
        public void run() {
            
            while(count <= 10){
                word1();
                word2();
            }   
        }
        
        private synchronized void word1(){
            try{
                Thread.sleep(200);
            }
            catch(InterruptedException e){
                System.out.println(e.toString());
            }
            word = "Mihi";
            System.out.print(word);
            count++;
        }
        
        private synchronized void word2(){
            
            try{
                Thread.sleep(200);
            }
            catch(InterruptedException e){
                System.out.println(e.toString());
            }
            word = "Churb\n";
            System.out.println(word);
            count++;
        }
    }
    
    public class TestThread {
    
        public static void main(String[] args) {
            TestSync test = new TestSync();
            Thread job1 = new Thread(test);
            job1.start();
        }
    }
     
    Last edited: