0

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(). Now: first performed method A(), then performed method C(), then performed method B(), then performed method A(), then performed method C(), then method B(), ...

But I want: first performed method A(), then performed method B(), then performed method C(), then A(), B(), C(), ... How can to do it? I just have access to Thread_Simple (Thread.currentThread()), How can I get Thread_Main from Thread.currentThread()?

Paushchyk Julia
  • 516
  • 3
  • 8
  • 24
  • your question is very abstract :/ – SabaRish Feb 19 '15 at 11:34
  • 1
    If things have to run in a specific serial order then why are you using threads in the first place? – Ian Roberts Feb 19 '15 at 13:17
  • When you want Sequential Order , Then what is the purpose of using threads? Use a sequential code instead ....... – Neeraj Jain Feb 19 '15 at 13:25
  • If you want to insure that methods A(), B(), and C() are called sequentially in a particular order, the way to do that is to call them all from a single thread. Threads are only beneficial in a program where there are some operations that can be safely performed _in any order_. There will always be _some_ synchronization between threads, but the more you use, the less benefit you will get from threading. If you force all of the threads to run in lock-step, then you will get no benefit from threading at all, but you will still incur many of the risks associated with threading. – Solomon Slow Feb 19 '15 at 14:52

2 Answers2

0

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?

Eni
  • 115
  • 8
0

you can use join method for this.

public class Test {
public static void main(String[] args) {
ThreadSample threadSample=new ThreadSample();
threadSample.start();
}
}

class Sample{
//Function a
public static void a(){
    System.out.println("func a");
}
//Function b
public static void b(){
    System.out.println("func b");
}
//Function c
public static void c(){
    System.out.println("func c");
}
}

class ThreadSample extends Thread{
@Override
public void run() {
    ThreadMain threadMain=new ThreadMain();
    threadMain.start();
    try {
        threadMain.join();
    } catch (InterruptedException e) {
        //handle InterruptedException
    }
    //call function c
    Sample.c();
}
}
class ThreadMain extends Thread{
@Override
public void run() {

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

output:

func a
func b
func c
Deepak
  • 43
  • 4
  • And you are sure that writing code in `sqeuential order` will enforce the `Threads` to maintian sequential order ...... ? What if during execuition of method `a()` , context switch happens and method `b()` starts executing – Neeraj Jain Feb 19 '15 at 13:30
  • since both function a() and b() is getting called from run method of same thread. so, they will maintain the order in which they are called. – Deepak Feb 20 '15 at 05:26
  • Are you trying to say that if 'method a' takes 15seconds to execute , then your thread is going to wait for '15seconds' before calling method b.... Its time to brush up some basic java skills – Neeraj Jain Feb 20 '15 at 05:31
  • yes. it doesn't matter how long a() will take, b() will start only after completion of a(). – Deepak Feb 20 '15 at 06:06
  • for satisfaction you can try running above sample program by replacing function a() with this one. public static void a(){ for(long i=0;i<9999999;i++) System.out.println("func a"); } – Deepak Feb 20 '15 at 06:08
  • 1
    In a `multithreading environment` , there will not always be only a `single thread` , Yes you were right that all method call are in `run method` of same thread but if only single thread is there then you don't even need to **join** , put all `method's call` same run method they will execute in order . You can check the same with your own example . Hope you got the point i wanted to explain – Neeraj Jain Feb 20 '15 at 06:22