水神骑士联盟吧 关注:4贴子:334
  • 12回复贴,共1
线程调度常用方法:
join方法在哪个线程执行哪个线程等待
Thread.yield()该线程放弃本次cpu的使用权,重新开始抢夺cpu的使用权
setDaemon(true)设置线程为守护线程
stop()强行停止线程(过时)不建议使用
interrupt() 通过让sleep() wait()方法抛出一个InterruptedException异常来中断线程的阻塞状态,并不能停止线程
-----------------------------------------------------------------------
生命周期:
线程创建
线程就绪:有执行资格,没有cpu的使用权
线程执行:有执行资格,有cpu的使用权限
线程阻塞:调用sleep()和wait()方法后线程处于阻塞状态,没有执行资格,没有cpu的使用权限
线程死亡


IP属地:河北1楼2016-08-16 22:21回复
    //死锁现象
    private static Object lock1 = new Object() ;
    private static Object lock2 = new Object() ;
    /**
    * 死锁现象
    * @param args
    */
    public static void main(String[] args) {
    new Thread(){
    public void run() {
    synchronized (lock1) {
    try {
    sleep(1000);//等待另一个线程拿到lock2使现象更明显
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(getName()+":lock1");
    synchronized (lock2) {
    System.out.println(getName()+":lock2");
    }
    }
    }
    }.start();
    new Thread(){
    public void run() {
    synchronized (lock2) {
    System.out.println(getName()+":lock2");
    synchronized (lock1) {
    System.out.println(getName()+":lock1");
    }
    }
    }
    }.start();
    }


    IP属地:河北7楼2016-08-17 10:09
    回复
      2025-06-06 14:19:07
      广告
      wait()和notify()方法只能在对应的同步代码块,由对应的方法执行,否则报错!


      IP属地:河北8楼2016-08-17 10:35
      回复
        线程生命周期图解:


        IP属地:河北9楼2016-08-17 10:48
        回复
          sleep()和join()与wait()的区别:
          该方法都可以使线程进入阻塞状态,但在同步代码块中,锁对象调用wait()方法进入线程阻塞状态将会释放掉锁对象的占用,而sleep()和join不会释放锁对象的占用


          IP属地:河北11楼2016-08-17 10:54
          收起回复
            关于线程组:
            没有指定线程组创建的线程默认属于main线程组
            通过Thread的构造方法指定该线程属于哪个线程组
            ThreadGroup tg = new ThreadGroup("tg-1") ;创建一个线程组
            方法:
            tg.setDaemon(true);设置该组下所有线程为后台线程
            tg.setMaxPriority(10);设置该组下所有线程的优先级
            tg.interrupt();中断该组下所有线程


            IP属地:河北12楼2016-08-17 11:15
            收起回复
              关于线程池的使用:
              public class Demo18 {
              public static void main(String[] args) {
              ExecutorService es = Executors.newFixedThreadPool(2) ;
              es.submit(new Runnable() {//使用线程
              @Override
              public void run() {
              while(true)
              System.out.println("2333");
              }
              }) ;
              es.submit(new Runnable() {
              @Override
              public void run() {
              while(true)
              System.out.println("2444");
              }
              }) ;
              es.submit(new Runnable(){//线程池不会根据增加创建新的线程,而是等待有空闲的线程时,再执行该任务
              @Override
              public void run() {
              while(true)
              System.out.println("2555");
              }
              }) ;
              /*//创建只有单个线程的线程池
              ExecutorService es = Executors.newSingleThreadExecutor() ;
              es.submit(new Runnable() {//使用线程
              @Override
              public void run() {
              while(true)
              System.out.println("2333");
              }
              }) ;
              es.submit(new Runnable() {//线程池不会根据增加创建新的线程,而是等待上一个任务执行结束后,再执行该任务
              @Override
              public void run() {
              while(true)
              System.out.println("2444");
              }
              }) ;*/
              }
              }


              IP属地:河北13楼2016-08-17 13:20
              收起回复
                关于线程池使用Callable接口开启一个异步任务
                ExecutorService es = Executors.newSingleThreadExecutor() ;
                Future<String> future = es.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                Thread.sleep(2000);
                return "2333";
                }
                }) ;
                long start = System.currentTimeMillis();
                try {
                String str = future.get() ;//调用此方法后main线程会等待 future对应的线程执行结束返回结果
                System.out.println(str);
                } catch (InterruptedException e) {
                e.printStackTrace();
                } catch (ExecutionException e) {
                e.printStackTrace();
                }
                System.out.println(System.currentTimeMillis()-start);


                IP属地:河北14楼2016-08-17 13:27
                收起回复
                  2025-06-06 14:13:07
                  广告
                  关于Timer定时器的使用:
                  public static void main(String[] args) {
                  Timer timer = new Timer() ;
                  TimerTask timerTask = new TimerTask() {
                  @Override
                  public void run() {
                  System.out.println("666666666");
                  }
                  } ;
                  // timer.schedule(timerTask, 1000);//在指定时间延迟后开始执行该定时任务
                  timer.scheduleAtFixedRate(timerTask, new Date(), 1000);//在指定日期开始执行定时任务并以1000毫秒为周期执行
                  timerTask.cancel();//取消定时任务
                  timer.purge();//清理以取消的定时任务(不包括已经执行完成的定时任务)
                  timer.cancel();//关闭定时器
                  }


                  IP属地:河北15楼2016-08-17 20:15
                  回复