Skip to content Skip to sidebar Skip to footer

How Can I Get Main Thread From Thread.currentthread()?

I am sorry, I have stupid question. I have two threads. Thread_Main and Thread_Simple, in Thread_Main performed method A() and method B(). In Thread_Simple performed method C(). N

Solution 1:

This is normally done using a thread lock. This enforces all the Methods in one thread to be completed before another thread can be executed. Can you provide code ?also slightly confused, what do you mean by you only have access to one thread?

Solution 2:

you can use join method for this.

publicclassTest {
publicstaticvoidmain(String[] args) {
ThreadSample threadSample=new ThreadSample();
threadSample.start();
}
}

classSample{
//Function apublicstaticvoida(){
    System.out.println("func a");
}
//Function bpublicstaticvoidb(){
    System.out.println("func b");
}
//Function cpublicstaticvoidc(){
    System.out.println("func c");
}
}

classThreadSampleextendsThread{
@Override
publicvoidrun() {
    ThreadMain threadMain=new ThreadMain();
    threadMain.start();
    try {
        threadMain.join();
    } catch (InterruptedException e) {
        //handle InterruptedException
    }
    //call function c
    Sample.c();
}
}
classThreadMainextendsThread{
@Override
publicvoidrun() {

    //call function a
    Sample.a();
    //call function b
    Sample.b();
}
}

output:

funcafuncbfuncc

Post a Comment for "How Can I Get Main Thread From Thread.currentthread()?"