public class MultiThreadDemo
{
public static void main(String args[])
{
NamePrinter jacky = new NamePrinter("Jacky");
NamePrinter mickel = new NamePrinter("Michel");
NamePrinter jone = new NamePrinter("Jone");

Thread thread1 = new Thread(jacky);
Thread thread2 = new Thread(mickel);
Thread thread3 = new Thread(jone);

thread3.setPriority(Thread.MAX_PRIORITY);

thread1.start();
thread2.start();
thread3.start();
}
}class NamePrinter implements Runnable
{
private String name;

public NamePrinter(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void run()
{
int i=0;
while(true)
{   //输出时,这里为什么每次i=某个值时,每个线程名都会输出
                       //我的理解是i每次会自增,换一个线程,就i就会增加一次。
                      //不可能说i已经++过了,换一个线程在这个地方先断下。
    System.out.println("\n\ni = "+    i++      +"  name = "+name);
         System.out.println("ThreadName = "+Thread.currentThread().getName()); try
{
Thread.sleep(2000);
}catch(InterruptedException e)  //这个是什么异常,什么时候触发
{

}
}
}
}

解决方案 »

  1.   

    因为对多线程来说。第一步都是在就绪状态,也就是说在你的第一个线程
    执行的时候。对于其他的线程是在等待状态,但是你第一个线程一旦睡眠,
    就会执行其他的线程,第一次,第一个线程输出的i=0,线程名字,然后它睡眠,
    第二个线程又会继续执行,同样的他的第一次也是i=0,线程名字,同理,后面的线程一样
    ,所以你每次i=某个值时,每个线程名都会输出
    InterruptedException  当线程在很长一段时间内一直处于正在等待、休眠或暂停状态,而另一个线程用 Thread 类中的 iterrupt 方法中断它时,抛出该异常。 
    就是说你设定的是两秒的时间睡眠,但是没到两秒的时候你用其他的方法中断了这个线程,就会报这个异常
      

  2.   

    这个你理解的有错误呀,,
    你是在run方法里面去定义的一个局部变量,,每次你Thread thread2 = new Thread(mickel); 这样实例化了一个线程,是不错,,
    然后执行start方法之后才去用线程里面的run方法,,
    方法内部的变量是局部变量,当你开始执行这个方法时才存在,执行完了这个方法之后,,他就消失了,,
      

  3.   

    主要是 Thread.sleep(2000);起了作用,
    让你的第1个线程休眠 阻塞,就会跳到别的线程,其它的线程执行也是同理。
    还有如果你想让所有线程共用i的话 ,就在run()方法外面将i定义成,静态变量static。
    InterruptedException 一楼讲的好 ,顶下他的。
      

  4.   

    1.每个线程的变量只属于他本身,是相互独立的,除非你使用的是共享资源。比如文件流操作
    2. InterruptedException 是中断异常,当你中断还在阻塞的线程时就会抛这个异常,但是有些线程是无法中断的,比如锁。
    建议:
    添加中断判断
    用1.5的线程类库可以减少错误
      

  5.   

    关于变量i的问题。
    是因为楼主没有搞清楚这个变量的作用域。
    线程类的非静态私有成员变量,其作用域只限线程的方法本身(包括run方法);
    当然,上述程序的i变量,其作用域只限线程本身,而且只限run方法的方法体内,
    int i=0;这条语句的后面。所以,其他线程是无法访问的。楼主可以试一下,把其改成静态的成员变量。
    也就是说,不在run方法里面声明,在类里面声明,并且是静态的变量。
    由于是多线程访问的公有变量,所以还要使用volatile关键字。
    如:private static volatile int i=0;
      

  6.   

    InterruptedException 
    当线程在很长一段时间内一直处于正在等待、休眠或暂停状态,而另一个线程用 Thread 类中的 iterrupt 方法中断它时,抛出该异常。
      

  7.   

    不用静态变量,楼主可以试试这个:public class MultiThreadDemo {
    public static void main(String args[]) {
    ThisString ts=new ThisString();
    NamePrinter jacky = new NamePrinter(ts);
    NamePrinter mickel = new NamePrinter(ts);
    NamePrinter jone = new NamePrinter(ts); Thread thread1 = new Thread(jacky);
    Thread thread2 = new Thread(mickel);
    Thread thread3 = new Thread(jone);
    thread3.setPriority(Thread.MAX_PRIORITY);
    thread1.start();
    thread2.start();
    thread3.start();
    }
    }class NamePrinter implements Runnable {
    private ThisString name; public NamePrinter(ThisString name) {
    this.name = name;
    }
    public ThisString getName() {
    return name;
    }
    public void run() {
    while (true) {
    System.out.println("\n\ni = " + name.i++ + "  name = " + name);
    System.out.println("ThreadName = " + Thread.currentThread().getName());
    try {
    Thread.sleep(2000);
    } catch (InterruptedException e){ }
    }
    }
    }class ThisString {
    int i=0;
    }
      

  8.   

    说白了就是,变量i是每一个NamePrinter对象的私有变量,所以jacky对应的线程里的i   ++了,不等于另外两个的i也++了,楼主明白了没?
      

  9.   

    就好象这样:package myTry;public class test
    {
        public static void main(String[] args)
        {
            B b1=new B();
            B b2=new B();
            B b3=new B();
            System.out.println(b1.i++);
            System.out.println(b2.i++);
            System.out.println(b3.i++);
            System.out.println(b1.i++);
            System.out.println(b2.i++);
            System.out.println(b3.i++);
        }
    }class B
    {
    int i;
    }线程也是一个对象,它也可以有私有变量,这里你就把线程当成一个普通类的对象理解就行了
      

  10.   

    你这个例子其实可以看作你同时指派n个人去做某件事。这n个人做这件事的方法都是一样的,各做各的,每个人都有自己的变量i.所以,他们打印变量i的顺序是随机的。InterruptedException 是当线程无法正常唤醒时,调用interrupte方法后,如果线程能够唤醒就会抛出这个错误。
      

  11.   

    呵呵,1楼2楼合起来就全了,你用new方法产生了3个不同的对象,i是三个不同对象中的变量,当然也是三个不同的i,自然每个线程调用的时候相应对象的i就加1啦,
    另外:1楼翻译的非常正确,原文是这样说的:
    InterruptedException - if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown
      

  12.   


    单个线程可以看做是类实例化的对象,例子种3个线程就是3个对象,每个线程拥有自己的属性和方法,3个线程是独立的。i和name属性是都是私有的,不是共有的.
      

  13.   

    因为对多线程来说。第一步都是在就绪状态,也就是说在你的第一个线程 
    执行的时候。对于其他的线程是在等待状态,但是你第一个线程一旦睡眠, 
    就会执行其他的线程,第一次,第一个线程输出的i=0,线程名字,然后它睡眠, 
    第二个线程又会继续执行,同样的他的第一次也是i=0,线程名字,同理,后面的线程一样 
    ,所以你每次i=某个值时,每个线程名都会输出 
    InterruptedException  当线程在很长一段时间内一直处于正在等待、休眠或暂停状态,而另一个线程用 Thread 类中的 iterrupt 方法中断它时,抛出该异常。 
    就是说你设定的是两秒的时间睡眠,但是没到两秒的时候你用其他的方法中断了这个线程,就会报这个异常
    同意这些观点。
    我觉得多线程对于图形图象处理最为有用。。
      

  14.   

    这个你理解的有错误呀,, 
    你是在run方法里面去定义的一个局部变量,,每次你Thread thread2 = new Thread(mickel); 这样实例化了一个线程,是不错,, 
    然后执行start方法之后才去用线程里面的run方法,, 
    方法内部的变量是局部变量,当你开始执行这个方法时才存在,执行完了这个方法之后,,他就消失了,, 
      

  15.   

    public class MultiThreadDemo {
    public static void main(String args[]) {
    NamePrinter jacky = new NamePrinter("Jacky");
    NamePrinter mickel = new NamePrinter("Michel");
    NamePrinter jone = new NamePrinter("Jone"); Thread thread1 = new Thread(jacky);
    Thread thread2 = new Thread(mickel);
    Thread thread3 = new Thread(jone);// thread3.setPriority(Thread.MAX_PRIORITY); thread1.start();
    thread2.start();
    thread3.start();
    }
    }class NamePrinter implements Runnable {
    private String name;
    static int i = 0;
    public NamePrinter(String name) {
    this.name = name;
    } public String getName() {
    return name;
    } public void run() {
    // int i = 0;
    while (true) { // 输出时,这里为什么每次i=某个值时,每个线程名都会输出
    // 我的理解是i每次会自增,换一个线程,就i就会增加一次。
    // 不可能说i已经++过了,换一个线程在这个地方先断下。
    synchronized(System.out){ 
    System.out.println("\n\ni = " + i++ + "  name = " + name);
    System.out.println("ThreadName = "
    + Thread.currentThread().getName()); try {
    Thread.sleep(1000);
    } catch (InterruptedException e) // 这个是什么异常,什么时候触发
    { }}
    }
    }
    }
      

  16.   

    NamePrinter jacky = new NamePrinter("Jacky");
    NamePrinter mickel = new NamePrinter("Michel");
    NamePrinter jone = new NamePrinter("Jone");
    因为你new 了三个对象,于是i 就有了三份拷贝,
    你应改成
    NamePrinter np=new NamePrinter("np");
    Thread thread1 = new Thread(np,"jacky");
    Thread thread2 = new Thread(np,"mickel");
    Thread thread3 = new Thread(np,"jone");
      

  17.   

    我的同学说:
     Thread启动三个独立的线程,它们的循环,互相不扰。
    是不是这样??
      

  18.   

    每一个线程都有自己独立的资源,和自己唯一的id等等
    所以你启动的多个线程相互不干扰,除非你调用一些线程控制函数去限制执行。比如 
         把函数体置于同步块、,那么启动的多个线程某一时刻就只能有一个线程访问i,
    i就成了他们的共享变了,每一个线程的行为都会影响其他线程的结果。[自我理解,参考]
      

  19.   

    这样就好了,你要弄清楚多线程环境下变量是线程独有的还是线程共享的,这个很重要。public class MultiThreadDemo
    {
        public static void main(String args[])
        {
            NamePrinter jacky = new NamePrinter("Jacky");
            NamePrinter mickel = new NamePrinter("Michel");
            NamePrinter jone = new NamePrinter("Jone");
            
            Thread thread1 = new Thread(jacky);
            Thread thread2 = new Thread(mickel);
            Thread thread3 = new Thread(jone);
            
            thread3.setPriority(Thread.MAX_PRIORITY);
            
            thread1.start();
            thread2.start();
            thread3.start();        
        }
    }class NamePrinter implements Runnable
    {
        private String name;
        
        private static int i = 0;
        
        public NamePrinter(String name)
        {
            this.name = name;
        }
        public String getName()
        {
            return name;
        }
        public void run()
        {
            while(true)
            {   System.out.println("\n\ni = "+    i++      +"  name = "+name);
                System.out.println("ThreadName = "+Thread.currentThread().getName());            try
                {
                    Thread.sleep(2000);
                }catch(InterruptedException e)  //这个是什么异常,什么时候触发
                {
                    
                }
            }
        }
    }
      

  20.   

    或者这样:public class MultiThreadDemo
    {
        public static void main(String args[])
        {
            NamePrinter jacky = new NamePrinter("Jacky");
            
            Thread thread1 = new Thread(jacky);
            Thread thread2 = new Thread(jacky);
            Thread thread3 = new Thread(jacky);
            
            thread3.setPriority(Thread.MAX_PRIORITY);
            
            thread1.start();
            thread2.start();
            thread3.start();        
        }
    }class NamePrinter implements Runnable
    {
        private String name;
        private int i = 0;
        
        public NamePrinter(String name)
        {
            this.name = name;
        }
        public String getName()
        {
            return name;
        }
        public void run()
        {
            while(true)
            {   //输出时,这里为什么每次i=某个值时,每个线程名都会输出
                           //我的理解是i每次会自增,换一个线程,就i就会增加一次。
                          //不可能说i已经++过了,换一个线程在这个地方先断下。
                System.out.println("\n\ni = "+    i++      +"  name = "+name);
                    System.out.println("ThreadName = "+Thread.currentThread().getName());            try
                {
                    Thread.sleep(2000);
                }catch(InterruptedException e)  //这个是什么异常,什么时候触发
                {
                    
                }
            }
        }
    }
      

  21.   

    如果我在这程序中硬是加入Synichnized应该对程序没有影响吧.....
    他们不交互的.
    是不是一个循环必须结束,才给启动另一个线程????
      

  22.   

    这个例子中加不加synchronized是无所谓的。
    线程执行的过程是可能交叉的,并不是一个执行完了才执行另一个。
    你可以在循环中打印Thread.currentThread().getName()看看输出结果。