Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java Thread example

farenda 2015-09-30 0

Problem:

How to create a task running as a Java Thread? Thread and Runnable are the most basic concurrent types to use. In this post we show how to use Thread to execute concurrent tasks.

Solution:

In the previous post you could learn how to correctly use Runnable interface. In this post we show how to create a task that directly extends from java.lang.Thread class and run two instances to do their jobs concurrently:

package com.farenda.java.lang;

public class ThreadExample {

    private static class Counter extends Thread {

        // it's only to generate unique ids for each task:
        private static int threads = 0;

        private int id = ++threads;
        private final int number;

        public Counter(int number) {
            this.number = number;
        }

        @Override
        public void run() {
            for (int i = 0; i < number; ++i) {
                System.out.printf("counter %d, value: %d%n", id, i);
            }
        }
    }

    public static void main(String[] args) {
        Counter counter1 = new Counter(5);
        Counter counter2 = new Counter(5);

        System.out.println("Starting counters:");
        counter1.start();
        counter2.start();
        System.out.println("main thread ends here");
    }
}

Whole task implementation is performed run() method. Note that threads are not started when instantiated, but only after call to start() method.

And here’s the output of the above program:

Starting counters:
counter 1, value: 0
counter 1, value: 1
counter 1, value: 2
counter 2, value: 0
counter 2, value: 1
counter 2, value: 2
counter 2, value: 3
counter 2, value: 4
main thread ends here
counter 1, value: 3
counter 1, value: 4

As you can see the execution of threads is non-deterministic and can occur in any order – here part of the first thread job has been executed after thread two.

Also note that the program finishes only when all the threads have finished the work.

Share with the World!
Categories Java Tags java, java-concurrency
Previous: Java Runnable
Next: Bash select

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored