Research on Java Multithreading _ programming model

Source: Internet
Author: User

Topic: Java Multithreading _ Programming model Research
Author: Kagula
Date: 2007-12-27

Objective: To explore a simple way of working, and realize the coding of multithreading.

Reading objects: Understanding Java Common multithreaded concepts of readers.

Applicable to: Java beginners, haha, because, I also because of the need for work, at the beginning of the first look at Java, haha.

Environment:
[1] jdk1.6.x
[2] myeclipse5.1.x
[3] multicore or multiprocessor computers

Keywords:
Multithreading, common threads, main thread

Body:
[A] goal: High load operation allows independent threads (normal threads) to be implemented. The main thread can query, independent threading work.
This is a non-RT multithreaded system discussed here. Here, the spatial efficiency of the algorithm is not considered.

[A-1] I use the code (here, the method body T_main) that consumes a lot of CPU time as a method called the Mythread class.
The Msgmanagement class, which encapsulates Mythread, inherits from the Threadgroup class, so all the properties of the Wired Group (threadgroup).
When the main thread needs to call the method body T_main and completes the operation function, the Msgmanagement instance is manipulated with the following code fragment.
...
Object Lockmain=new object (); Used to lock the main thread, which can be used to wait for branch thread to return the result of the operation.
Msgmanagement mm=new msgmanagement ("MsgProc1", Lockmain); The instantiation of Msgmanagement
...
try {
Threadlistener tl=new Threadlistener (); Used to print, T_main running state.
String pthreadname =mm.invoke ("T_main",//with a large number of operations, method body name. Used to find ways to become independent threads.
New class[] {integer.class,long.class,threadlistener.class},//method body entry parameters, type list
New object[] {new Integer (3), New Long (4), TL}); Entry parameters for method body, list of values
...
The Msgmanagement instance creates a Mythread instance thread and starts it.

Here is the Mythread instance, the T_main method body
Public String T_main (
Integer pinteger,long Plong,threadlistener Objlistener)//Method body entry parameter, entry object list (some textbooks, call it the formal parameter list)
{
int ncount=100*1000*1000; Suppose that the amount of computation

Performing Operation Operations
... Partial code, slightly ...
Objlistener.setstate (...); /used, print operation status
... Partial code, slightly ...

               //return result of Operation
                Long lr=pinteger* Plong;
  return lr.tostring ();
 }

        The main thread, after completing its work, can invoke the Msgmanagement instance, the following code, to take the result of the Pthreadname thread's operation.
                          ... Partial code, slightly ...
    log.debug ("Wait for thread execution complete .... ");
    while (Mm.activecount () >0)   {
     mm.setmaxpriority ( thread.max_priority);
     synchronized (lockmain)
     {
       Lockmain.wait ();
    }
   }
   //Fetch threading Results
    log.debug (pthreadname+ "=" +mm.getresult (pthreadname). ToString ()); &NBSP
                          ... Partial code, slightly ...

We can also pass control information to T_main by Threadlistener class instances, so that the thread that encapsulates the T_main method ends early.
We can also use the Threadlistener class instance to take the T_main running state.

[A-2] This pattern, which encapsulates an operation that requires a separate thread to be performed as a mythread, may be more adaptable to starting a service group, such as a WebService server thread.
In the Java exploration, unfortunately, Java does not support pointers, so that some of the language can be used in C programming skills, in Java is difficult or not directly implemented. And Java also restricts the programming mode, the
One step limits the software design pattern, as we all know, the design pattern does not have the so-called good or bad, the sole criterion that judges it, only in the concrete project, this way is not able to finish the goal with the quickest quality.
However, there is no doubt that the ease of use of Java has overshadowed its limitations in flexibility.

Attention:
[1] Only on normal threads, the main thread (program) ends when the work is finished.

Terminology list:
[1] RT (Realtime) Chinese meaning: real-time, timely

Related information:
[1] Java theory and Practice: Hey, where's my thread? "
http://www.ibm.com/developerworks/cn/java/j-jtp0924/index.html
[2]" Writing multithreaded Java applications "
http:// Www.ibm.com/developerworks/cn/java/j-thread/index.html
[3] Java Advanced: multicore thread-volatile principles and techniques
http:// www.cn-java.com/www1/?action-viewnews-itemid-4347
[4] "Fix the Java memory model"
http://www.ibm.com/developerworks/cn/ JAVA/J-JTP02244/
[5] Real Time Java series
http://www.ibm.com/developerworks/cn/java/j-rtj/ 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.