**6. Example of simple class hierarchy represented by sequential object Petri net**

In this section we will demonstrate how simple hierarchy of the classes via sequential object Petri nets is built. The model of those examples involve selected classes contained in standard library of classes of the Java programming language. (e.g. classes **java.lang.Object**, **java.lang.Thread**, etc.). For better visibility of layouts we usually present individual declared methods in separate figures representing individual parts of the particular net page (while naturally places and transitions of the net which appear in individual parts of the net page and have identical identifiers, always represent the same place or transition of the total page of the net). Thus, top of our hierarchy of classes will be the class **Object**, whose declaration made in the Java programming language is the following:

```
public class Object { 
 private Object monitor; 
 private Thread thread; 
 private Vector<Thread> = new Vector<Thread>; 
 private synchronized static int getPointer() { … } 
 public Object() { … } 
 protected void finalize() { … } 
 public void lock() { … } 
 public void unlock() { … }
```

```
 public void wait() { … } 
 public void notify() { … } 
}
```
The static method **getPointer** (see Figure 19) can be executed by the only one programming thread only and it is determined for assigning unique integral values of pointers to individual instances of the classes. At the first execution of the method the integral value 1 is returned in variable **P** (because *InputBind*(#p) = 0 a @(#p) = @(<0>) = 1), in the place **Object.P1** is then stored the token **<0, 0>**, and at next execution of the method is in the variable **P** returned the value 2, etc.

Sequential Object Petri Nets and the Modeling of Multithreading Object-Oriented Programming Systems 219

while the programming thread is permitted to enter the critical section of the same object several times in sequence. First entry of the programming thread **T** into the critical section of the object **O** is realized by execution of the transition **Object.T5**. Pointer to the programming thread, which successfully obtained the object monitor, is then stored in the variable **thread** (i.e. the token **<T, O, 1>** is located in the place **Object.thread**). At repeated entry of the programming thread into the critical sections of the object **O** the transition **Object.T6** is executed and particular token in the place **Object.thread** is added with the next element which is the number **1** (i.e. at second entry of the thread **T** into the critical section of the object **O** is in the place **Object.thread** stored the token **<T, O, 1, 1>**, at the third entry the token **<T, O, 1, 1, 1>**, etc). Deal location of the object monitor and initialization of its critical section is then realized by execution of the non-static method **unlock**, whose functionalities

Method **wait** causes the current thread **T** to wait until another thread invokes the **notify** method for the object **O**. The current thread **T** must own the monitor of the object **O**. The thread releases ownership of this monitor and waits in the place **Object.pool** until another thread notifies threads waiting on the object's monitor to wake up through a call to the notify method. The thread **T** then waits in the place **Object.P3** until it can re-obtain

Next declared class in our hierarchy will be the class **Thread** representing the programming thread, which is the subclass of the class **Object** and it also implements the interface *Runnable* (see Figure 23). Its declaration in the Java programming language is as follows:

are inverse to functionalities of the method **lock** (see Figure 21).

**Figure 21.** Declaration of class **Object** in sequential object Petri net

**Figure 22.** Declaration of class **Object** in sequential object Petri net

ownership of the object monitor and resumes execution (see Figure 22).

**Figure 19.** Declaration of class **Object** in sequential object Petri net

Within the declaration of the constructor of the object of the class **Object** (see Figure 20) is by the programming thread firstly obtained the value of the pointer for a newly created object by calling the method **getPointer**, that value is then stored to the variable **O** and the value of non-static data item **monitor** representing the monitor of particular instance of the class is then stored in the form of the token **<O>** in the place **Object.monitor**. The destructor of the object represented by the method **finalize** will then mainly ensure cancellation of the monitor of the object represented by the token **<O>** in the place **Object.monitor**.

**Figure 20.** Declaration of class **Object** in sequential object Petri net

Entry into the critical section of the object (i.e. execution of the non-static method with the modifier **synchronized**) is conditioned by getting the object monitor with the particular programming thread. As a part of the execution of the method **lock** the programming thread **T** can respectively allocate the object monitor with the value of the pointer **O**, (i.e. the token **<O>** in the place **Object.monitor**) and then enter into the critical section of the object while the programming thread is permitted to enter the critical section of the same object several times in sequence. First entry of the programming thread **T** into the critical section of the object **O** is realized by execution of the transition **Object.T5**. Pointer to the programming thread, which successfully obtained the object monitor, is then stored in the variable **thread** (i.e. the token **<T, O, 1>** is located in the place **Object.thread**). At repeated entry of the programming thread into the critical sections of the object **O** the transition **Object.T6** is executed and particular token in the place **Object.thread** is added with the next element which is the number **1** (i.e. at second entry of the thread **T** into the critical section of the object **O** is in the place **Object.thread** stored the token **<T, O, 1, 1>**, at the third entry the token **<T, O, 1, 1, 1>**, etc). Deal location of the object monitor and initialization of its critical section is then realized by execution of the non-static method **unlock**, whose functionalities are inverse to functionalities of the method **lock** (see Figure 21).

**Figure 21.** Declaration of class **Object** in sequential object Petri net

218 Petri Nets – Manufacturing and Computer Science

The static method **getPointer** (see Figure 19) can be executed by the only one programming thread only and it is determined for assigning unique integral values of pointers to individual instances of the classes. At the first execution of the method the integral value 1 is returned in variable **P** (because *InputBind*(#p) = 0 a @(#p) = @(<0>) = 1), in the place **Object.P1** is then stored the token **<0, 0>**, and at next execution of the method is in the variable **P**

Within the declaration of the constructor of the object of the class **Object** (see Figure 20) is by the programming thread firstly obtained the value of the pointer for a newly created object by calling the method **getPointer**, that value is then stored to the variable **O** and the value of non-static data item **monitor** representing the monitor of particular instance of the class is then stored in the form of the token **<O>** in the place **Object.monitor**. The destructor of the object represented by the method **finalize** will then mainly ensure cancellation of the

Entry into the critical section of the object (i.e. execution of the non-static method with the modifier **synchronized**) is conditioned by getting the object monitor with the particular programming thread. As a part of the execution of the method **lock** the programming thread **T** can respectively allocate the object monitor with the value of the pointer **O**, (i.e. the token **<O>** in the place **Object.monitor**) and then enter into the critical section of the object

monitor of the object represented by the token **<O>** in the place **Object.monitor**.

 **public void wait() { … } public void notify() { … }** 

returned the value 2, etc.

**Figure 19.** Declaration of class **Object** in sequential object Petri net

**Figure 20.** Declaration of class **Object** in sequential object Petri net

**}** 

Method **wait** causes the current thread **T** to wait until another thread invokes the **notify** method for the object **O**. The current thread **T** must own the monitor of the object **O**. The thread releases ownership of this monitor and waits in the place **Object.pool** until another thread notifies threads waiting on the object's monitor to wake up through a call to the notify method. The thread **T** then waits in the place **Object.P3** until it can re-obtain ownership of the object monitor and resumes execution (see Figure 22).

**Figure 22.** Declaration of class **Object** in sequential object Petri net

Next declared class in our hierarchy will be the class **Thread** representing the programming thread, which is the subclass of the class **Object** and it also implements the interface *Runnable* (see Figure 23). Its declaration in the Java programming language is as follows:

```
public class Thread extends Object implements Runnable { 
 private Runnable runnable; 
 public Thread(Runnable run) { … } 
 protected void finalize() { … } 
 public void start() { … } 
 public void run() { … } 
}
```
The method **Thread** is the constructor of the object which during its execution firstly invokes the construction of the instance of the class **Object**, that is the superclass of the class **Thread**, which in the variable value **U** returns the pointer to the newly created programming thread. In the place **Thread.runnable** (i.e. in the value of the variable **runnable**) is then stored the token **<U, pRun>**, where the value of the variable **pRun** contains the pointer to the class instance implementing the interface *Runnable* (see Figure 18) whose method **run** will be then executed by the newly created programming thread. That will be implemented through execution of the method **start** with programming thread **T**. The token **<U, pRun, 0>** is then located into the place **Thread.runnable** representing the fact that the execution of the method **pRun.run** was initiated. Its invocation itself is realized by asynchronous method call by the firing of the transition **Thread.T3** and by the mechanism of multiarc (**Thread.T3**, **Thread.Start**). The class **Thread** itself has the method **run** implemented the easiest possible way.

Sequential Object Petri Nets and the Modeling of Multithreading Object-Oriented Programming Systems 221

Next declared class in our hierarchy will be the class **Semaphore** representing a counting semaphore. Conceptually, a semaphore maintains a set of permits. Method **down** blocks if necessary until a permit is available, and then takes it. Method **up** adds a permit, potentially releasing a blocking acquirer. However, no actual permit objects are used and the **Semaphore** just keeps a count of the number available and acts accordingly. Declaration of

The constructor of the object, i.e. the method **Semaphore**, following execution of the constructor of the instance of the superclass **Object** will store in the place **Semaphore.value** the token **<O, 1>** representing the initial value of the data item value of the class instance **O**. As a part of the execution of the destructor of the instance of the class **Semaphore**, i.e. of the method **finalize**, that token is removed from the place **Semaphore.value** and then the

**Figure 24.** Declaration of class **Thread** in sequential object Petri net

the class in the Java programming language is the following:

destructor of the superclass **Object** is executed (see Figure 25).

**Figure 25.** Declaration of class **Semaphore** in sequential object Petri net

**public class Semaphore extends Object {** 

 **public synchronized void down() { … } public synchronized void up() { … }** 

 **private int value = 1; public Semaphore() { … } protected void finalize() { … }** 

**}** 

**Figure 23.** Declaration of class **Thread** in sequential object Petri net

As a part of execution of the destructor of the instance of the class **Thread**, i.e. the method **finalize** (see Figure 24), the particular token is removed from the place **Thread.runnable** and the destructor of the superclass **Object** is then executed. If in the place **Thread.runnable** is the token of the form **<U, pRun>**, the programming thread is not active to obeyed destruction and the transition **Thread.T7** can be fired. If, on the contrary, in the place **Thread.runnable** is the token in the form **<U, pRun, 0>**, the programming thread is active and the execution of the destructor must be delayed until the completion of the activities of this thread (i.e. returning of the thread **U** after executing the method **pRun.run** ensured by the mechanism of the multiarc (**Thread.finalize**, **Thread.T8**)).

**Figure 24.** Declaration of class **Thread** in sequential object Petri net

Next declared class in our hierarchy will be the class **Semaphore** representing a counting semaphore. Conceptually, a semaphore maintains a set of permits. Method **down** blocks if necessary until a permit is available, and then takes it. Method **up** adds a permit, potentially releasing a blocking acquirer. However, no actual permit objects are used and the **Semaphore** just keeps a count of the number available and acts accordingly. Declaration of the class in the Java programming language is the following:

```
public class Semaphore extends Object {
```
220 Petri Nets – Manufacturing and Computer Science

 **public Thread(Runnable run) { … } protected void finalize() { … } public void start() { … } public void run() { … }** 

**run** implemented the easiest possible way.

**Figure 23.** Declaration of class **Thread** in sequential object Petri net

the mechanism of the multiarc (**Thread.finalize**, **Thread.T8**)).

 **private Runnable runnable;** 

**}** 

**public class Thread extends Object implements Runnable {** 

The method **Thread** is the constructor of the object which during its execution firstly invokes the construction of the instance of the class **Object**, that is the superclass of the class **Thread**, which in the variable value **U** returns the pointer to the newly created programming thread. In the place **Thread.runnable** (i.e. in the value of the variable **runnable**) is then stored the token **<U, pRun>**, where the value of the variable **pRun** contains the pointer to the class instance implementing the interface *Runnable* (see Figure 18) whose method **run** will be then executed by the newly created programming thread. That will be implemented through execution of the method **start** with programming thread **T**. The token **<U, pRun, 0>** is then located into the place **Thread.runnable** representing the fact that the execution of the method **pRun.run** was initiated. Its invocation itself is realized by asynchronous method call by the firing of the transition **Thread.T3** and by the mechanism of multiarc (**Thread.T3**, **Thread.Start**). The class **Thread** itself has the method

As a part of execution of the destructor of the instance of the class **Thread**, i.e. the method **finalize** (see Figure 24), the particular token is removed from the place **Thread.runnable** and the destructor of the superclass **Object** is then executed. If in the place **Thread.runnable** is the token of the form **<U, pRun>**, the programming thread is not active to obeyed destruction and the transition **Thread.T7** can be fired. If, on the contrary, in the place **Thread.runnable** is the token in the form **<U, pRun, 0>**, the programming thread is active and the execution of the destructor must be delayed until the completion of the activities of this thread (i.e. returning of the thread **U** after executing the method **pRun.run** ensured by

```
 private int value = 1; 
 public Semaphore() { … } 
 protected void finalize() { … } 
 public synchronized void down() { … } 
 public synchronized void up() { … } 
}
```
The constructor of the object, i.e. the method **Semaphore**, following execution of the constructor of the instance of the superclass **Object** will store in the place **Semaphore.value** the token **<O, 1>** representing the initial value of the data item value of the class instance **O**. As a part of the execution of the destructor of the instance of the class **Semaphore**, i.e. of the method **finalize**, that token is removed from the place **Semaphore.value** and then the destructor of the superclass **Object** is executed (see Figure 25).

**Figure 25.** Declaration of class **Semaphore** in sequential object Petri net

Method **down** acquires a permit from this semaphore, blocking until one is available. Following entry into the critical section of the object instance the programming thread acquires a permit from this semaphore (ie. token in the place **Semaphore.value** can be bound to the arc sequence **<O, 1, #x>** after at least one execution of the method **up**) and will leave the critical section of the object. If no permit is available then the current thread becomes disabled for thread scheduling purposes and waits (i.e. the transition **Semaphore.T7** is executed and the method **Object.wait** is invoked, the programming thread will release the monitor of the object in order to enable execution of the method **up** by other programming thread) until some other thread invokes the **up** method for this semaphore and the current thread is next to be assigned a permit (see Figure 26).

Sequential Object Petri Nets and the Modeling of Multithreading Object-Oriented Programming Systems 223

Sequential object Petri nets represent an interesting class in the area of object Petri net classes, which can be applied at design, modeling, analysis and verification of generally distributed multithreading object-oriented programming systems. A newly introduced term of token as finite non-empty recursive sequence over the set of non-negative integer numbers, functionalities of multiarcs and the mechanism of the firing of transitions do not increase demands on performance of analysis of characteristics, as seen in other classes of

Functional programming is one of the most important paradigms of programming that looks back on a long history. The recent interest in functional programming started as a response to the growing pervasiveness of concurrency as a way of scaling horizontally. Multithreaded programming is difficult to do well in practice and functional programming offers (in many ways) better strategies than object-oriented programming for writing robust, concurrent software. Functional programming is generally regarded a paradigm of programming that can be applied in many languages - even those that were not originally intended to be used with that paradigm. Like the name implies, it focuses on the application of functions. Functional programmers use functions as building blocks to create new functions and the function is the main construct that architecture is built from. Several programming languages (like Scala, C#, Java, Delphi, etc) are a blend of object-oriented and functional programming concepts in a statically typed language in the present time. The fusion of object-oriented and functional programming makes it possible to express new kinds of programming patterns and component abstractions. It also leads to a legible and concise programming style. Sequential object Petri nets then also fully support design, modeling, analysis and verification of programming systems based on this fusion of object-

This paper has been elaborated in the framework of the IT4Innovations Centre of Excellence project, reg. no. CZ.1.05/1.1.00/02.0070 supported by Operational Programme 'Research and Development for Innovations' funded by Structural Funds of the European Union and state

Agha, G. A.; Cinindio, F. & Rozenberg, G. (2001). *Concurrent Object-Oriented Programming and Petri Nets: Advances in Petri Nets*. Springer, ISBN 978-3-540-41942-6, Berlin, Germany

**7. Conclusion** 

high-level or colored Petri Nets.

oriented and functional programming paradigms.

*VŠB-Technical University of Ostrava, Czech Republic* 

**Author details** 

**Acknowledgement** 

**8. References** 

budget of the Czech Republic.

Ivo Martiník

**Figure 26.** Declaration of class **Semaphore** in sequential object Petri net

Method **up** releases a permit, increasing the number of available permits by one. If any threads are trying to acquire a permit, then one is selected and given the permit that was just released. That thread is (re)enabled for thread scheduling purposes (see Figure 27).

**Figure 27.** Declaration of class **Semaphore** in sequential object Petri net

Sequential Object Petri Nets and the Modeling of Multithreading Object-Oriented Programming Systems 223
