Write multithreaded applications with PB

zhaozj2021-02-17  73

Multithreading refers to multiple processes performed simultaneously within a program. It compared to single-threading: first, it can better utilize system resources, such as CPU, if a thread is thread I / O Operation is blocked, and the other thread can still use the CPU to execute; second, it better meets the customer's needs, because picky customers want you to develop the program while playing music, displaying files, download Network file, etc., this is a single-threaded application that cannot be completed. Currently, development tools that support multi-threaded are mainly: Java, VC, Delphi, and C Builder. Multithreading emphasizes that there are multiple processes inside the process to execute at the same time, and the concept of simultaneous execution is equivalent to asynchronous processing in the PB distributed calculation. That is, as long as we implement asynchronous processing inside a program, it is equivalent to achieving multithreading. To this, how to do it next step: distributed computing in one application, using server push technology, asynchronously performs functions in the shared object. Next, a DEMO is prepared, introducing how to develop multi-threaded applications in PB, which is shown in Figure 1. Uo-Thread1 and Uo-Thread2 are two instances of UO-Thread, UO-Thread, and the functionality of UO-Thread is from the instance variable Li-count, from 5 seconds, self-adding simultaneously UO-Argv1 and UO-Argv2 Send a UE-THREAD message. The function of the two intermediate objects is to transfer the UE-Thread message to UE-Thread1 and UE-Thread2 to the main window W-main. Figure 1 After the Program Structure The main window receives the UE-Thread1 message, the value of the own variable amount in UO-Thread1 is received, and the value of the alternating variable in UO-Thread2 is displayed. In a single thread, UO-Thread1 is executed first, and the UO-Thread2 is executed after 5 seconds, so the main window can only receive the UE-Thread1 message in the first 5 seconds, and only UE-Thread2 messages can only be received within 5 seconds. . In multi-thread, Uo-Thread1 and Uo-Thread2 are simultaneously executed, so W-main can constantly receive UE-Thread1 and UE-Thread2 messages. 1. Design User Object Uo-Thread New Class User Object, name UO-Thread, add the following two instance variables: NonvisualObject inv-arg long li-count // Create the following three user functions: (1 )Uf-start () The function is to complete the self-add 5 seconds and send a UE-THREAD message to the intermediate object. Script is: time t0 t0 = now () // Get the current time do while secondsafter (t0, now ()) <= 5 li-count // instance variable self-add 5 second inv-arg.triggerevent ('ue-thread' ) // Send a UE-THREAD message to the intermediate object loop (2) () (), which is: Get the instantaneous value of the self-altered variable. Its script is: return Li-count // Returns instance variables (3) EF-SETPARENT (NonVisualObject NV-ARG), when calling, using an intermediate object as a parameter.

The script is: INV-ARG = NV-ARG // assigns an instance variable to instance variables with an intermediate object 2. Design Intermediate Object UO-ARGV1 and UO-Argv2 New Class User Object, name UO-Argv1, add the following instance variable: Window Win-arg creates user functions uf-setparent (window w-argv), when calling, with the main window As a parameter. Its script is: win-arg = W-argv // with a main window object assigning a user event UE-Thread to respond to UE-Thread messages issued by UO-Thread1, with a script: Win-Arg .Triggerevent ("UE-Thread1") // Send UE-Thread1 messages to the main window UO-ARGV2 and UO-Argv1, simply change UE-Thread1 in UO-ARGV1 to UO-Thread2. 3. Design the main window W-main main window appearance As shown in Figure 2, the three controls of the concave are: St-Thread1, St-Thread2, ST-TIME, which are used to display UO-Thread1 and UO-Thread2 Current instantaneous value and system time of variables. Declare two user events: UE-Thread1 and UE-Thread2 are used to respond to UE-Thread1 and UE-Thread2 messages sent by intermediate objects.

转载请注明原文地址:https://www.9cbs.com/read-31066.html

New Post(0)