How to create Thread in java

Tuesday, January 5th, 2016 (0 Comment)

1. What is Thread in java ?

Thread is a command-line that CPU must execute. The new operating system allows multiple threads to be executed simultaneously. We’re used to open multiple applications in one time worked on computer –> Many applications are loaded.

2. Benefits of use multiple Thread in java

Increased CPU utilization: Most of execution time of the application is waiting for input from the user -> CPU performance using less effective.

Creating consistency between objects: for example in one game, the characters together moving. In one Web page, created by the simultaneity of the road fringing as the title bar (banner, text, graphics, run), just show the clock, just playing, just play the game, animated images, … -> Website eye-catching and captivating.

Manage time in applications such as online competition, or time playing a game.

3. Programming thread in Java with 2 option:

Option 1: Build one subclass of java.lang.Thread, override acts run () to fit the purpose of problem.

Option 2: Build one class that implements the interface Runnable.

3.1. Example create thread with java.lang.Thread class

Output:

3.2. Example create thread with interface Runnable

Output:

3.3. Example ues some methods of Thread

Output:

3.4. Example about status of Thread

Output:

4. Priority of Thread in java

The Threads sharing time of CPU -> Thread in last queues will waiting long time to be executed CPU -> The changing needs of Thread priority. Java provides three constants describing the priority of one Thread (use the other priority use integers from 1 .. 10).

  • NORM_PRIORITY=5
  • MAX_PRIORITY=10
  • MIN_PRIORITY=1

The default priority of a Thread is NORMAL PRIORITY. Child Thread the same priority with parent Thread (so by inheritance).

Example priority of Thread in java

Output:

Tags: , ,

Related Posts

core java
Export Crystal Report with Subreport in java
core java
Convert xml to object java and vice versa
core java
Java basic tutorial on lvtutorial.com
core java
Java hibernate manytomany to manage user role

Related Posts

core java
Export Crystal Report with Subreport in java
core java
Convert xml to object java and vice versa
core java
Java basic tutorial on lvtutorial.com
core java
Java hibernate manytomany to manage user role

Leave a Reply

Your email address will not be published. Required fields are marked *