java - Two threads and two CPU -


i have struggle understand following situation: (i working in java) let's say, have object o , 2 different threads t1 , t2 , 2 cpu. if t1 calls o.method1 , t2 calls o.method2 @ same time, can cpu1 run o.method1 , cpu2 o.method2 @ same time?

yes, can. however, nothing guarantees different threads executed in parallel. have guarantee executed concurrently.

the operating system free bind threads 1 cpu (and switch cpus of threads). done time.

in particular, when other applications running cpu-intensive computations, 2 threads may share same cpu. in case, threads run interleaved.

(in answer, said cpu applies cpu cores.)

of course, locks , synchronization can make 1 thread wait on other. still technically, waiting can interpreted running, although not consuming cpu time.

also note executing methods in parallel (usually) has nothing on which objects call methods. in example, calling 2 different methods on same object. however, irrelevant. except if both methods markes synchronized in java, introduces synchronization (see paragraph above) on particular object , make 1 thread wait until other has finished call on method.


Comments