认识Thread读书笔记

认识java里面的线程
    1. 线程实现的3种方法
a extends Thread
       b 实现runnable接口
        c implements Callable(面试说出来就nb了)
       例子:第三种实现方法类似runnable
public class TestThread implements Callable<String> {
@Override
public String call() throws Exception {
// TODO Auto-generated method stub
return  "this is thread";
}
public static void main(String[] args) {
TestThread b =new TestThread();
FutureTask<String> f = new FutureTask<>(b);
new Thread(f).start();
System.out.println("this is a main thread");
try {
System.out.println("得到的结果是:"+f.get());
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}   
线程的中断机制
    1.调用Thread.stop() 该方法强迫终止一个线程,并抛出一个新创建的threadDeath对象作为异常,停止一个未启动的线程
是被允许的,它会立即停止,如果catch子句捕获了一个ThreadDeath对象,则必须重新抛出该对象,线程才会真正停止。
Thread.stop不安全,已经不建议使用
2.第二种 Thread.interrupt()方法和机制,中断机制是一种协作机制,也就是说通过中断并不能直接终止另一个线程
而需要被中断的线程自己处理中断。
例子:
package com.test.ll;
public class ThreadDemo implements Runnable {
// let main thread intertupted threaddemo
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new ThreadDemo(), "interruptThreadDemo");
thread.start();
thread.sleep(3000);
System.out.println("interrupting thread ...");
thread.interrupt();
System.out.println("thread is interrupting" + thread.isInterrupted());
thread.sleep(3000);
System.out.println("stopping application");
}
@Override
public void run() {
boolean stop = false;
while (!stop) {
System.out.println("my thread is running...");
long time = System.currentTimeMillis();
while ((System.currentTimeMillis() - time) < 1000) {
}
if (Thread.currentThread().isInterrupted()) {
break;
}
}
System.out.println("exit");
}
}
关于线程的生命周期
    线程生命周期的五种状态
   新建:创建一个thread
就绪:runnable,线程已经被启动,正在等待被cpu分给时间片,如果没有分配到时间片,那么他不一定会运行,此时线程处于线程的就绪队列,等待系统为其分配cpu,等待状态不是执行状态,此时线程处于是活着的。
堵塞: 由于某种原因导致让出cpu并让cpu并暂停自己的执行,
      a 正在睡眠
       b 正在等待,
       c 被另一个线程所阻塞,调用suspend()方法,可调用resume()方法恢复。
死亡:当线程执行完毕,活着被其他线程杀掉。
什么是守护线程?
    守护线程可以简单理解为后台线程,进程结束,守护线程自然地就结束,例如程序运行时播放的北京音乐
启动守护线程,setDaemon(boolean on) //该方法必须在启动线程前调用
当前线程副本 :threadlocal

例子:

       public class ThreadLocalTest {

private ThreadLocal<Integer> seqNum = new ThreadLocal<Integer>() {

protected Integer initialValue() {
return 0;
};
};
public ThreadLocal<Integer> getSeqNum() {
return seqNum;
}
public int getNextSeqNum() {
seqNum.set(seqNum.get() + 1);
return seqNum.get();
}
public static void main(String[] args) {
ThreadLocalTest test =new ThreadLocalTest();
TestClient a1=new TestClient(test);
TestClient a2=new TestClient(test);
TestClient a3=new TestClient(test);
a1.start();
a2.start();
a3.start();
}
@SuppressWarnings("unused")
private static class TestClient extends Thread {
private ThreadLocalTest test;
@SuppressWarnings("unused")
public TestClient(ThreadLocalTest test) {
super();
this.test = test;
}
@Override
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println("current thread" + Thread.currentThread().getName() +"====="+ test.getNextSeqNum());
}
test.getSeqNum().remove();
}
}
}
线程异常的处理
    run 方法不允许throw exception 所有的异常必须在run方法内进行处理
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值