黑马程序员技术交流社区

标题: 多线程的问题 [打印本页]

作者: ぺsimon☆    时间: 2013-4-23 11:03
标题: 多线程的问题
本帖最后由 ぺsimon☆ 于 2013-4-25 01:26 编辑
  1. class StopThread implements Runnable
  2. {
  3.          boolean flag=true;
  4.         public synchronized void run() //定义一个同步函数
  5.         {
  6.                 if(flag)
  7.         while(true) //如果flag等于true执行循环体
  8.         {
  9.         //try{this.wait();}catch(Exception e){}  //处理异常
  10.         
  11.         
  12.         for(int x=0;x<60;x++)
  13.         {
  14.         System.out.println(Thread.currentThread().getName()+"---run run"+x);
  15.         
  16.         }
  17.         }
  18.         else
  19.         while(true)
  20.         show();
  21.         }
  22.         
  23.         public synchronized void show()
  24.         {
  25.                 for(int x=0;x<60;x++)

  26.         System.out.println(Thread.currentThread().getName()+"---show run"+x);

  27.         }
  28.         public void ChangeFlag()
  29.         {
  30.          flag=false; //返回 flag=false
  31.         }
  32. }

  33. class StopThreadDemo
  34. {
  35.         public static void main(String[] args)
  36.         {
  37.         StopThread st=new StopThread(); //创建对象
  38.         Thread t1=new Thread(st); //创建一个线程t1
  39.         t1.start(); //开启线程t1
  40.         try{Thread.sleep(20);}catch(Exception e){}
  41.         st.flag=false;
  42.         Thread t2=new Thread(st); //创建一个线程t2
  43.         
  44.         t2.start(); //开启线程t2
  45.         /*
  46.         for(int x=0;x<60;x++)
  47.         {

  48.         if(x==20)
  49.         {
  50.                 st.ChangeFlag(); //调用ChangeFlag()方法
  51.         }
  52.         
  53. //        t1.interrupt();//调用interrupt()方法,结束线程的冻结状态
  54.         System.out.println("main-----"+x);
  55.         
  56.         }
  57.         */
  58.         }
  59. }
复制代码
问题:
为什么程序运行后只有一个线程在运行呢?

作者: 陈湘林    时间: 2013-4-23 11:33
多线程
1,线程的基本概念
程序运行的基本单位,一个程序的不同执行通道
在同一个时间点,cpu只能处理一个线程
每一个线程都有自己的空间
2,线程的创建和启动
第一种:继承Thread类
run方法启动
第二种:实现Runnable接口
run方法启动
注意,线程的启动,必须用Thread.start()静态方法启动
两种方法实现线程,区别不大,只是用接口会灵活些
3,线程状态间转换
调用线程start方法,处于就绪状态,等待cpu处理,然后进入运行状态,
如果run方法没有执行完毕,进入阻塞状态
4,线程控制的基本方法
boolean        isAlive()判断线程是“活着”还是“终止”

int        getPriority()返回线程的优先级数值

void        setPriority(int newPriority)更改线程的优先级

static void        sleep(long millis)在指定的毫秒数内让当前正在执行的线程休眠(暂停执行)。

void        join()插队

static void        yield()礼让

void        interrupt()中断线程。半路线程停止

还有俩方法
wait(),等待
notifyAll(),唤醒

作者: 林声荣    时间: 2013-4-23 12:15
可能是for(int x=0;x<60;x++)中x的循环太短了,cpu执行速度很快,你的t2线程还没抢到cpu执行权,t1线程就把线程执行完了,你试一下将循环增大点看行不


作者: 沈浩    时间: 2013-4-23 13:07
因为你的while循环体的问题  仔细看看  当thread0进入while循环体 拿到锁后  一直在里执行语句   就算thread1获得cpu执行权  它也不能执行show()方法啊  因为
thread0根本就没释放锁啊     所以你就会看到只有一个线程在执行    你可以加入等待唤醒  效果就不一样了
作者: ぺsimon☆    时间: 2013-4-23 14:49
沈浩 发表于 2013-4-23 13:07
因为你的while循环体的问题  仔细看看  当thread0进入while循环体 拿到锁后  一直在里执行语句   就算threa ...

我要线程0打印   run run
线程1打印  show  run该怎么做呢
作者: 沈浩    时间: 2013-4-23 17:27
ぺsimon☆ 发表于 2013-4-23 14:49
我要线程0打印   run run
线程1打印  show  run该怎么做呢


             if(flag)   
        while(true) //如果flag等于true执行循环体
        {
        if(!flag)
        try{this.wait();}catch(Exception e){}  //处理异常
         for(int x=0;x<60;x++)
        {
        System.out.println(Thread.currentThread().getName()+"---run run"+x);
        this.notify();
                flag=false;
        }
        }
       else
       while(true)
        show();
        }
        public synchronized void show()
        {
               if(flag)
                        try{this.wait();}catch(Exception e){}
                 for(int x=0;x<60;x++)
        System.out.println(Thread.currentThread().getName()+"---show run"+x);
                        this.notify();
                        flag=true;
        }
作者: 黄玉昆    时间: 2013-4-24 22:50
如果问题未解决,请继续追问,如果问题解决了,请将问题分类改为“已解决”,谢谢




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2