Answer to Question #227534 in Java | JSP | JSF for umme habiba sumi

Question #227534

Write a java program that executes three threads. First thread displays Good Morning everyone second. Second thread displays Hello every two seconds and the third thread displays Welcome in every three seconds. Create the three threads by extending the thread class. 


1
Expert's answer
2021-08-20T14:17:03-0400
class A extends Thread {
    synchronized public void run() {
        try {
            int i=0;
            while (i<5) {
                sleep(1000);
                System.out.println("Good morning ");
                i++;
            }
        } catch (Exception e) {
        }
    }
}
 
class B extends Thread {
    synchronized public void run() {
        try {
            int i=0;
            while (i<5) {
                sleep(2000);
                System.out.println("hello");
                i++;
            }
        } catch (Exception e) {
        }
    }
}
 
class C extends Thread {
    synchronized public void run() {
        try {
            int i=0;
            while (i<5) {
                sleep(3000);
                System.out.println("welcome");
                i++;
            }
        } catch (Exception e) {
        }
    }
}
 
class Main {
    public static void main(String args[]) {
        A a = new A();
        B b = new B();
        C c = new C();
        a.start();
        b.start();
        c.start();
     
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS