关于线程:
其实我们写Java的第一个程序就已经使用了线程,运行Hello World程序时,控制作用的主线程将被创建。
关于多线程:
当我们使用一个多线程的程序时,线程并不可能真正地独立运行,不管讨论多处理机还是并行处理,任何时候都真正只有一个线程在运行。这是因为每个线程都被分配了一小块CPU执行的时间块,接着它必须等待其他线程的运行。只不过是因为CPU处理速度快,以至于好像是多个线程在同时执行。
关于制作线程类:
我们有两种方法:1.扩展线程类;2实现Runnable接口。如果能选接口的尽量学接口,因为选了扩展线程类就没有那么灵活了,就不能继承其他父类了,选接口就不同了。我们可以实现多个接口,这样的可拓展性就比较强。
关于线程的生命周期:
一个线程完整的生命周期包括下面5种状态:新建状态,可运行状态,运行状态,不可运行状态,死亡状态。
有3中情况导致一个线程不能进入可运行状态:调用了sleep方法,调用了wait状态,正在等待I/O。
关于线程同步:
生产者/消费者问题是线程同步的典型问题,我们需要使用synchronized关键字来获得这种保护。
生产者/消费在问题的代码:
public class TestThread {
public static void main(String[] args) {
SyncStack ss = new SyncStack();
Producer p = new Producer(ss);
Consumer c = new Consumer(ss);
new Thread(p).start();
new Thread(p).start();
new Thread(p).start();
new Thread(c).start();
}
}
class WoTou {
int id;
WoTou(int id) {
this.id = id;
}
public String toString() {
return "WoTou : " + id;
}
}
class SyncStack {
int index = 0;
WoTou[] arrWT = new WoTou[6];
public synchronized void push(WoTou wt) {
while(index == arrWT.length) {
try {
System.out.println("满了!");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
arrWT[index] = wt;
index ++;
}
public synchronized WoTou pop() {
while(index == 0) {
try {
this.wait();
System.out.println("空了!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
index--;
return arrWT[index];
}
}
class Producer implements Runnable {
SyncStack ss = null;
Producer(SyncStack ss) {
this.ss = ss;
}
public void run() {
for(int i=0; i<20; i++) {
WoTou wt = new WoTou(i);
ss.push(wt);
System.out.println("生产了:" + wt);
try {
Thread.sleep((int)(Math.random() * 200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
SyncStack ss = null;
Consumer(SyncStack ss) {
this.ss = ss;
}
public void run() {
for(int i=0; i<20; i++) {
WoTou wt = ss.pop();
System.out.println("消费了: " + wt);
try {
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
这是别人写的,参照下,
关于线程安全:
我们都是使用synchronized关键字来锁定,把这个区域保护起来,这种临界区可以安全地被一个应用程序的多个线程使用访问共享数据。但是我们要防止死锁,不能让一个线程要使用的资源被其他线程锁定,而这个线程又锁定了其他线程完成所需的资源。例如线程A完成需要资源1和资源2,线程B也需要资源1和资源2;如果线程A先锁定了资源1,而线程B先锁定了资源2,这样就造成了死锁。
这是我对线程的一点认识,不足之处,请多多指教。