Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Learn More

Test Thread - Java (Basic) Certification Solution | HackerRank

1 min read

Note: This solution is only for reference purpose. Feel free to use this solution as inspiration and enhance your knowledge but please don't literally copy and paste the code.


Solution of the problem:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class SampleDemo implements Runnable {
    private Thread t;
    private String threadName;

    SampleDemo(String threadName) {
        this.threadName = threadName;
    }

    @Override
    public void run() {
        while (true) {
            System.out.println(threadName);
        }
    }

    public void start() {
        if (t == null) {
            t = new Thread(this, threadName);
            t.start();
        }
    }
}

public class TestThread {
    public static void main(String[] args) {
        SampleDemo A = new SampleDemo("A");
        SampleDemo B = new SampleDemo("B");
        B.start();
        A.start();
    }
}



Labels : #hackerrank certification ,#java (basic) ,

Post a Comment