OS Basics

Multiple Thread (Runnable vs. Thread), Part 1

  1. Multi-threading states
  2. Java thread init methods
  3. Examples of Thread and Runnable

Multi-threading states

  1. new: 新建状态,保持这个状态直到程序start()
  2. ready: 调用了start(),就绪状态的线程处于就绪队列中,要等待JVM里线程调度器的调度
  3. running: 获取了CPU资源,执行run()里面的指令
  4. suspend: 失去所占用资源之后,该线程就从运行状态进入阻塞状态。在睡眠时间已到或获得设备资源后可以重新进入就绪状态。可以分为三种
    • 等待阻塞: wait()
    • 同步阻塞: 线程在获取synchronized同步锁失败后
    • 其他阻塞: 其他阻塞:通过调用线程的sleep()或join()发出了I/O请求时,线程就会进入到阻塞状态
  5. dead: 一个运行状态的线程完成任务或者其他终止条件发生时,该线程就切换到终止状态

Java thread init methods

Java 提供了三种创建线程的方法

  • 通过实现Runnable接口
  • 通过继承Thread类本身
  • 通过Callable和Future创建线程

Java thread example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// class extending Thread
import java.util.*;
// new class
class MyThread extends Thread {
private int ticket = 10;
private String name;
private Random rand = new Random();
// constructors
public MyThread(String name) {
this.name = name;
System.out.println(this.name + " starts with " + this.ticket + ".");
}
// Override the run function to implement functionality
@Override
public void run() {
for (int i = 0; i < 500; i++) {
if (this.ticket > 0) {
// create a random number between two sales
int nxt = rand.nextInt(20) * 10;
this.ticket--;
System.out.println(this.name + " sold a ticket, now has " + this.ticket + " tickets.");
try {
Thread.sleep(nxt);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("Now, " + this.name + " has ended.");
}
}
// method to run the class above
public class ThreadDemo {
public static void main(String[] args) {
System.out.println("start");
MyThread mt1 = new MyThread("Counter 1");
MyThread mt2 = new MyThread("Counter 2");
MyThread mt3 = new MyThread("Counter 3");
mt1.start();
mt2.start();
mt3.start();
}
}

Runnable Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.Random;
// now using Runnable interface instead of extending a class
public class MyRunnable implements Runnable{
private int ticket = 30;
private Random rand = new Random();
private Lock lock = new ReentrantLock();
public MyRunnable() {
System.out.println("This class starts with " + this.ticket + " tickets.");
}
// overriding the run method just as before
@Override
public void run() {
while (this.ticket > 0) {
// tryLock() checks if the lock is available, takes the lock and return true if yes, else return false
if (lock.tryLock()){
try {
// command to execute with the lock
System.out.println(Thread.currentThread().getName() + " acquired lock.");
this.ticket--;
System.out.println(Thread.currentThread().getName() + " sold a ticket, now has " + this.ticket + " tickets.");
} catch (Exception e) {
System.err.println(e);
} finally {
// releasing the lock
System.out.println(Thread.currentThread().getName() + " released lock.");
lock.unlock();
}
// wait for a random length after selling a ticket
int nxt = rand.nextInt(20) * 10;
try {
Thread.sleep(nxt);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// if lock is not available, wait
else {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("Now, " + Thread.currentThread().getName() + " has ended.");
}
}