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.

JavaState

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.

Snippets

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();
        firstThread.start();
        firstThread.run();
    }
}

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);
            thread.start();
        }
    }
}

Result of running:

Snippet2RunningResult

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)
    {
        super(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()");
        myThread.run();

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

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

Result of running:

Snippet3RunningResult

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

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇