The use of this development technique allowed us to cope highly complex simulations, with satisfactory results for clients and in very short time, which has lead in excellent results for the company.


  • A multithreaded program contains two or more parts that can run concurrently or simultaneously.
  • Each part of the program is called thread and each thread defines a separate path of execution.
  • It is a specialized form of multitasking.

The Java Thread Model

The Java interpreter and all class libraries are designed based on the multithreaded model to allow the whole environment to be asynchronous. This increases efficiency, preventing the waste of CPU cycles, this provides a significant advantage when calling the operating system resources because in the scheme of a single thread, when a program execution suspends waiting for a resource, the entire program stops. In multithreaded programming, this does not happen. For example, the downtime that occurs when a thread reads data from the network and waits for the user to enter this information can be used by another.

States of Java Threads

A thread can have the following states:

  • Execution: The thread is being executed.
  • Usually execution: The thread is in the ready list of threads to execute, as soon as each thread has time in the CPU.
  • Suspension: The thread execution is suspended temporarily.
  • Resumption: A suspended thread runs again continuing his task at the point where you left off.
  • Blocked: A thread is waiting for a resource.

At any time a thread can pause, ending execution immediately. And once stopped, can not be restarted.

Java ThreadsProperties

Priorities: Java assigns a priority that determines how to treat a thread in relation to others. The priority of the threads are defined by a range of integers (1 - 10) that specify the relative priority of a thread over another. The priority of a thread is used to decide when to switch from running one thread to the next. This is termed context switching. The rules for context switching are:

A thread can voluntarily yield the CPU access. This happens when a thread enters the following situations:

  • Terminates or makes an explicit abandonment,
  • He falls asleep, or blocked by an pending I/O. When this occurs, we examine all other threads, and assigns the CPU to be prepared to run and have the highest priority.

A thread can be evicted by other with higher priority. In this case a lower priority thread that does not release the processor is merely dislodged, regardless of what he is doing, by a second highest priority thread.Basically, when a higher priority thread wishes to execute, it will. This task is called eviction.

Multithreaded Programming in Java

The Thread class and Runnable interface

Java runs two ways to schedule threads using the Thread class or Runnable interface.

Thread class has several methods to help manage the threads. Below are some of them:

  • void start(): Thread execution begins. Invokes the run () method of the thread.
  • public void run(): It is the body of a running thread. This is the only method of the Runnable interface. It is called by the start() method after the appropriate thread system is initialized.
  • void stop():
  • static void sleep (long millis) : allows the thread specified wait time specified in parameter (in milliseconds). Give opportunity for any priority threads running.
  • static void yield (): allows threads that are runnable state and have the same priority of the thread that runs the yield implemented.

Only gives opportunity to have equal priority threads running.

  • void suspend():
  • public final void resume() : Resumes execution of a Thread that was previously suspended state.
  • public final boolean isAlive():
  • public final void setPriority(int newPriority): Sets the thread priority indicated by the value passed as a parameter. There are many predefined constants to the priority defined in the Thread class, such as MIN_PRIORITY, NORM_PRIORITY and MAX_PRIORITY, taking the values ​​1, 5 and 10, respectively.
  • public final int getPriority(): This method returns the priority of the current thread.
  • public final void setName( String ): This method identifies a row with a name. This will facilitate purification of multithreaded programs.
  • public final getName(): This method returns the current value, the name assigned to the current thread.

Example of threads created with the Thread class

The following program does two numbering from 0 to 10 using two different threads to know which thread prints the number on the screen we use the getName() method.

class HiloSencillo extend Thread {
public HiloSencillo (String str)
 public void run ()
    for (int i=0; i<10; i++)  {
     System.out.println(i + " " + getName());
     try  {
     Thread.sleep(int)(Math.random() * 1000));
     } catch(Exception e) {}


class EjemploDosHilos {
 public static viid main (String[] args) {
  new HiloSencillo ("Hola").start();
  new HiloSencillo ("Adios").start();
 } }

The result will look approximately the following:

Hilos en Java

Another way to create a thread is to create a class that implements the Runnable interface. The Runnable interface defines the Run method that must be implemented in the class you want to run in a separate thread to run the class, this should be passed as an argument to the constructor of the Thread class.

Below is the previous thread program but now using the runnable interface:

class HiloSencillo2 implements Runnable {
 private String nombre;
 public HiloSencillo2 (String str) {
 public String getName () {
  return this.nombre;
 public void run () {
  for (int i=0; i<10; i++) {
   System.out.println (i + " " + getName());
   try {
    Thread.sleep((int)(Math.random() * 1000));


class EjemploDosHilos {
 public static void main (String[] args) {
  new Thread(new HiloSencillo2("Hola")).start();
  new Thread(new HiloSencillo2("Adios")).start();

Its application in business

Recently, they asked us to develop two simulations, one for the control and operation of vehicles passing the Second Floor of Periférico in Mexico City, and another to present the toll flow from two points of view, the first when you have determined the number of lanes in a toll and entered the flow of all types of vehicles and the second is giving a capacity to be crossed by the various toll lanes in space, determine the size of the shed.

Development time was shorter but more importantly, the customer was satisfied, today we are applying this concept of development in several of our commitments that we discuss later on this website.


comments powered by Disqus