1

I am interested if it's possible to run 2 lines of code simultaniously,

let's say if i have some function funk(); funk1(); so i want them to be executed simultaniusly

user2672165
  • 2,986
  • 19
  • 27
Tony
  • 1,175
  • 4
  • 14
  • 22
  • 2
    http://stackoverflow.com/questions/4722974/threading-example-in-android i think this can help u – denza Mar 11 '12 at 17:11

2 Answers2

4

You can use Threads to achieve that easily. No need for separate processes (in most cases)

For example:

//in some method
Thread t1 = new Thread(new Runnable() {
    @Override
    public void run() {
        //function one or whatever
    }
});
t1.start();
Thread t2 = new Thread(new Runnable() {
    @Override
    public void run() {
        //function two or whatever
    }
});
t2.start();
Mohammad Rafigh
  • 757
  • 9
  • 17
MByD
  • 135,866
  • 28
  • 264
  • 277
  • @Tony - BTW, the link in the comment to your question is also great (once you are a bit more familiar with threads) – MByD Mar 11 '12 at 17:15
  • 1
    Just this one question by writing this code func1 in t1 and funk2 in t2 when t1.start()is called it will start executing funk1 and in the same time will continue to t2.start()and execute funk2 parallel? – Tony Mar 11 '12 at 17:20
  • 1
    Well maybe not in the exact millisecond, but yes, they will be executed in parallel. – MByD Mar 11 '12 at 17:26
  • can be truly in parallel on a multi-CPU device - there are a few now with dual , and 4 way coming soon – pootle Mar 11 '12 at 17:35
  • So `t1` and `t2` are started at around the same time? – Ruchir Baronia Feb 03 '16 at 05:07
  • not exactly, but close to one another, if you want to make them start even closer, move the call to `t1.start()` to right before `t2.start()`, and this way shorten the delay between their start. – MByD Feb 03 '16 at 08:25
0

this is the official answer of your question.

http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html

contains simple examples and good definitions.

Guillermo Tobar
  • 344
  • 4
  • 17