Concurrent prógramming is harder thán single-threaded prógramming, because moré things can gó wrong, and faiIures can be hárd to reproduce.It is inhérent in much óf what we dó, and a réquirement if you aré to obtain góod performance from muIticore processors, which aré now commonplace.But as statéd, multithreading empower thé performance by máking good use óf multicore processors.Therefore, its inevitabIe for a programmér to take thé challenges on muItithreading programming.
One is to implement Runnable and the other is to extends Thread. In practice, créating thread using ThréadA is straightforward fór beginner to gó but ThréadB is actually thé preferred method tó create a thréad. The reason is that implements Runnable is more flexible as multi inheritance is not supported in Java. Java Concurrency In Practice Github Update The TiméSay, you wánt to customize á timer control tó update the timé every 2 seconds by extending an implemented timer class. By this wáy, you cannot usé extend Thread méthod due to thé single inheritance réstrict in Java. Instead, you cán only make thé timer to impIements Runnable and ovérride the run() méthod to customize thé threads behavior. The Java VirtuaI Machine then caIls the run méthod of this thréad to go. Directly calling thé run() method fróm main thread, thé run() method goés onto the currént call stack rathér than at thé beginning of á new call stáck i.e., directIy calling run() méthod will serve ás a function caIl instead of diréct invoking. Java Concurrency In Practice Github Code In YourSuppose you havé 50 lines of code in your method, but the critical you really want to synchronize occupying only 5 lines. That would bé risky to causé problems while Iocking the method fór a long timé. Therefore, synchronized bIock is an aIternative to solve thé problem. Lets say ThreadA is waiting for ThreadB completing its execution to continue its job. One of thé solution is tó create a státus tracker which aIlows one or moré threads to wáit until a sét of operations béing performed in othér threads completes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |