Operating Systems: Lab 0 Threads

Operating Systems: Lab 0 Threads

This is probably not an Lab, just a little exercise.

Here you can know the basic feature of the thread, and try to create the first thread by yourself.

Thread in Java

How to create ?

If you want to creat a thread, you can through:
1. Extending Thread class
2. Implementing the Runnable interface

For example:

public class MyFirstThread extends Thread
public class MyFirstThread implements Runnable

This Class or Interface have some default methods or states, you can rewrite them as you see fit.

Java Thread State

It’s some different than what you learned about thread states, java has six states on thread:

  1. New
  2. Runnable

    Java don’t have Running state, when the thread is running, it still is Runnable.

  3. Blocked
  4. Terminated(Dead)
  5. Waiting
  6. Timed waiting

These state should to be switched by interrupt or different methods.


Two Methods in Java Thread

  1. start()
    It means start a new thread, the new thread will executes run() method, and the method will return immeditately, then go to next thread. The new thread will concurrent running.
    start() method cannot be called more than once.
  2. run()
    When we call the run() method, the current thread will execute run(), instead of starting a new thread.
    run() method can be called more than once.
    This methods is belong the Runnable interface.


Here three code snippets that you should practice. These will help you realize the thread in Java.

Snippet 1

In this snippet, a thread is created by ‘extends’, and rewrite the run() method.

Hera you can see the run() method was executed twice. This is because the start() will call run() by default.

public class MyFirstThread extends Thread{
    public void run()
        System.out.println("The thread was created!!!");
        System.out.println("Let me see some interesting!!");
        System.out.println("OH!!!NO!!! The thread will be closed!!");

    public static void main(String []args)
        MyFirstThread firstThread = new MyFirstThread();

Snippets 2

In this snippet, a thread is created by ‘implements’, and rewrite the run() method.

Java Thread has some default methods, here we use the:

  1. Thread() – constructor
    This snippet used the public Thread(Runnable target), it specify a new Thread object as target.
  2. setName()
    Set the name of the thread.
  3. currentThread()
    This methodd will reaturn the infomation of thread which be executing, and the returned value is a Thread object.
  4. getName()
    Get the name of the thread.

The target is to know the feature of the thread running.
Although you create many threads in order, the threads running are not in order.

public class Order implements Runnable{
    public void run()
        String threadName = Thread.currentThread().getName();
        System.out.println("Hello! I'm "+threadName);
    public static void main(String []args)
        Order order = new Order();
        for(int i=0;i<5;i++)
            Thread thread =  new Thread(order);
            thread.setName("Thread "+i);

Result of running:


This result is not unique, if you run this program many times, you may get different results.

Snippets 3

The super is a keyword about Java extends. The super() method will call the constuctor of parent class(superclass).So these is equal to Thread(name).

class MyThread extends Thread
    MyThread(String name)

    public void run()
        System.out.println(Thread.currentThread().getName()+" is running!");

public class Demo{
    public static void main(String []args)
        Thread myThread=new MyThread("firstThread");

        System.out.println(Thread.currentThread().getName()+" called myThread.run()");

        System.out.println(Thread.currentThread().getName()+" called myThread.start()");

        System.out.println(Thread.currentThread().getName()+" called myThread.run()");

Result of running:


The thread will be dead when the run() method exits. So, when you call run() again, the thread may be different.


发送评论 编辑评论

 ̄﹃ ̄
∠( ᐛ 」∠)_
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
( ๑´•ω•) "(ㆆᴗㆆ)
Source: github.com/k4yt3x/flowerhd