Thinking in java 4th Edition 读书笔记-Concurrency(3)
来源:互联网 发布:淘宝网卖风湿药品 编辑:程序博客网 时间:2024/05/16 10:21
1.However, implementing an interface does allow you to inherit from a different class, whereas inheriting from Thread does not.
Notice that start( ) is called within the constructor, but you should be aware that starting threads inside a constructor can be quite problematic, because another task might start executing before the constructor has completed, which means the task may be able to access the object in an unstable state. This is yet another reason to prefer the use of Executors to the explicit creation of Thread objects.
2.Although there are some coding variations, run() start() are an absolute must.
3.Sometimes it makes sense to hide your threading code inside your class by using an inner class
import java.util.concurrent.*;
import static net.mindview.util.Print.*;
// Using a named inner class:
class InnerThread1 {
private int countDown = 5;
private Inner inner;
private class Inner extends Thread {
Inner(String name) {
super(name);
start();
}
public void run() {
try {
while(true) {
print(this);
if(--countDown == 0) return;
sleep(10);
}
} catch(InterruptedException e) {
print("interrupted");
}
}
public String toString() {
return getName() + ": " + countDown;
}
}
public InnerThread1(String name) {
inner = new Inner(name);
}
}
// Using an anonymous inner class:
class InnerThread2 {
private int countDown = 5;
private Thread t;
public InnerThread2(String name) {
t = new Thread(name) {
public void run() {
try {
while(true) {
print(this);
if(--countDown == 0) return;
sleep(10);
}
} catch(InterruptedException e) {
print("sleep() interrupted");
}
}
public String toString() {
return getName() + ": " + countDown;
}
};
t.start();
}
}
// Using a named Runnable implementation:
class InnerRunnable1 {
private int countDown = 5;
private Inner inner;
private class Inner implements Runnable {
Thread t;
Inner(String name) {
t = new Thread(this, name);
t.start();
}
public void run() {
try {
while(true) {
print(this);
if(--countDown == 0) return;
TimeUnit.MILLISECONDS.sleep(10);
}
} catch(InterruptedException e) {
print("sleep() interrupted");
}
}
public String toString() {
return t.getName() + ": " + countDown;
}
}
public InnerRunnable1(String name) {
inner = new Inner(name);
}
}
// Using an anonymous Runnable implementation:
class InnerRunnable2 {
private int countDown = 5;
private Thread t;
public InnerRunnable2(String name) {
t = new Thread(new Runnable() {
public void run() {
try {
while(true) {
print(this);
if(--countDown == 0) return;
TimeUnit.MILLISECONDS.sleep(10);
}
} catch(InterruptedException e) {
print("sleep() interrupted");
}
}
public String toString() {
return Thread.currentThread().getName() +
": " + countDown;
}
}, name);
t.start();
}
}
// A separate method to run some code as a task:
class ThreadMethod {
private int countDown = 5;
private Thread t;
private String name;
public ThreadMethod(String name) { this.name = name; }
public void runTask() {
if(t == null) {
t = new Thread(name) {
public void run() {
try {
while(true) {
print(this);
if(--countDown == 0) return;
sleep(10);
}
} catch(InterruptedException e) {
print("sleep() interrupted");
}
}
public String toString() {
return getName() + ": " + countDown;
}
};
t.start();
}
}
}
public class ThreadVariations {
public static void main(String[] args) {
new InnerThread1("InnerThread1");
new InnerThread2("InnerThread2");
new InnerRunnable1("InnerRunnable1");
new InnerRunnable2("InnerRunnable2");
new ThreadMethod("ThreadMethod").runTask();
}
}
万变不离其宗,要么extends Thread 要么implements Runnable,run() start()都是必须具备的。首先生成一个Thread实例,构造实例时需要run()函数,然后用实例调用start().
4.If a thread calls t.join( ) on another thread t, then the calling thread is suspended until the target thread t finishes (when t.isAlive( ) is false).
5.(I don't understand, but it occurs. )When another thread calls interrupt( ) on this thread, a flag is set to indicate that the thread has been interrupted. However, this flag is cleared when the exception is caught, so the result will always be false inside the catch clause. The flag is used for other situations where a thread may examine its interrupted state apart from the exception.
6.One of the motivations for using threading is to create a responsive user interface.
- Thinking in java 4th Edition 读书笔记-Concurrency(3)
- Thinking in java 4th Edition 读书笔记-Concurrency(1)
- Thinking in java 4th Edition 读书笔记-Concurrency(2)
- Thinking in java 4th Edition 读书笔记-I/O(3)
- Thinking in java 4th Edition 读书笔记-I/O(4)
- Thinking in java 4th Edition 读书笔记-I/O(1)
- Thinking in java 4th Edition 读书笔记-I/O(2)
- Thinking in java 4th Edition 读书笔记-I/O(5)
- Thinking in java 4th Edition 读书笔记-I/O(6)
- thinking in java 4th 读书笔记
- Thinking in java 4th Edition 习题答案全
- Thinking in Java 4th edition 中文非扫描版
- 移位操作符(Thinking in Java 4th Edition)
- 求素数(Thinking in Java 4th Edition)
- Thinking in Java 4th
- Thinking in Java 4th
- Thinking in Java 4th
- Thinking in Java 4th
- 应用DIV CSS编码时很容易犯一些错误
- XFIRE入门
- ArrayList 用法(java)
- 如何重置MySQL超级账户的密码
- ArrayList和LinkedList性能测试
- Thinking in java 4th Edition 读书笔记-Concurrency(3)
- Gernally FI T-code list
- 自己写的阅读时使用的小工具
- 有关生产订单的成本分析
- 2950端口安全配置
- 前瞻3D显示技术
- 实战mysql修复
- 程序员的真情告白
- Hibernate报如下错误: org.hibernate.MappingException: Unknown entity: