# Task management

### Number of tasks, tasks id's

PCJ library offers two useful methods:

```java
public static int PCJ.threadCount()
```

which returns number of tasks running and

```java
public static int PCJ.myId()
```

which returns id of the task. Task id is integer value of the range from 0 to `PCJ.threadCount()-1`.

### Task synchronization

PCJ offers PCJ.barrier() method which allows to synchronize all tasks. While this line is reached, the execution is stopped until all tasks reach the synchronization line.

```java
public static void PCJ.barrier()
```

Remember, that this line has to be executed by all tasks. The user can provide argument to `barrier()` which is integer id of the task to synchronize.

```java
public static void PCJ.barrier(int id)
```

In this case two tasks are synchronized: one with the given id and one which starts `barrier()`method. Please note that both tasks have to execute method.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pcj.icm.edu.pl/manual/task-management.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
