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) //这个是什么异常,什么时候触发
{
}
}
}
}
{
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) //这个是什么异常,什么时候触发
{
}
}
}
}
执行的时候。对于其他的线程是在等待状态,但是你第一个线程一旦睡眠,
就会执行其他的线程,第一次,第一个线程输出的i=0,线程名字,然后它睡眠,
第二个线程又会继续执行,同样的他的第一次也是i=0,线程名字,同理,后面的线程一样
,所以你每次i=某个值时,每个线程名都会输出
InterruptedException 当线程在很长一段时间内一直处于正在等待、休眠或暂停状态,而另一个线程用 Thread 类中的 iterrupt 方法中断它时,抛出该异常。
就是说你设定的是两秒的时间睡眠,但是没到两秒的时候你用其他的方法中断了这个线程,就会报这个异常
你是在run方法里面去定义的一个局部变量,,每次你Thread thread2 = new Thread(mickel); 这样实例化了一个线程,是不错,,
然后执行start方法之后才去用线程里面的run方法,,
方法内部的变量是局部变量,当你开始执行这个方法时才存在,执行完了这个方法之后,,他就消失了,,
让你的第1个线程休眠 阻塞,就会跳到别的线程,其它的线程执行也是同理。
还有如果你想让所有线程共用i的话 ,就在run()方法外面将i定义成,静态变量static。
InterruptedException 一楼讲的好 ,顶下他的。
2. InterruptedException 是中断异常,当你中断还在阻塞的线程时就会抛这个异常,但是有些线程是无法中断的,比如锁。
建议:
添加中断判断
用1.5的线程类库可以减少错误
是因为楼主没有搞清楚这个变量的作用域。
线程类的非静态私有成员变量,其作用域只限线程的方法本身(包括run方法);
当然,上述程序的i变量,其作用域只限线程本身,而且只限run方法的方法体内,
int i=0;这条语句的后面。所以,其他线程是无法访问的。楼主可以试一下,把其改成静态的成员变量。
也就是说,不在run方法里面声明,在类里面声明,并且是静态的变量。
由于是多线程访问的公有变量,所以还要使用volatile关键字。
如:private static volatile int i=0;
当线程在很长一段时间内一直处于正在等待、休眠或暂停状态,而另一个线程用 Thread 类中的 iterrupt 方法中断它时,抛出该异常。
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;
}
{
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;
}线程也是一个对象,它也可以有私有变量,这里你就把线程当成一个普通类的对象理解就行了
另外:1楼翻译的非常正确,原文是这样说的:
InterruptedException - if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown
单个线程可以看做是类实例化的对象,例子种3个线程就是3个对象,每个线程拥有自己的属性和方法,3个线程是独立的。i和name属性是都是私有的,不是共有的.
执行的时候。对于其他的线程是在等待状态,但是你第一个线程一旦睡眠,
就会执行其他的线程,第一次,第一个线程输出的i=0,线程名字,然后它睡眠,
第二个线程又会继续执行,同样的他的第一次也是i=0,线程名字,同理,后面的线程一样
,所以你每次i=某个值时,每个线程名都会输出
InterruptedException 当线程在很长一段时间内一直处于正在等待、休眠或暂停状态,而另一个线程用 Thread 类中的 iterrupt 方法中断它时,抛出该异常。
就是说你设定的是两秒的时间睡眠,但是没到两秒的时候你用其他的方法中断了这个线程,就会报这个异常
同意这些观点。
我觉得多线程对于图形图象处理最为有用。。
你是在run方法里面去定义的一个局部变量,,每次你Thread thread2 = new Thread(mickel); 这样实例化了一个线程,是不错,,
然后执行start方法之后才去用线程里面的run方法,,
方法内部的变量是局部变量,当你开始执行这个方法时才存在,执行完了这个方法之后,,他就消失了,,
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) // 这个是什么异常,什么时候触发
{ }}
}
}
}
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");
Thread启动三个独立的线程,它们的循环,互相不扰。
是不是这样??
所以你启动的多个线程相互不干扰,除非你调用一些线程控制函数去限制执行。比如
把函数体置于同步块、,那么启动的多个线程某一时刻就只能有一个线程访问i,
i就成了他们的共享变了,每一个线程的行为都会影响其他线程的结果。[自我理解,参考]
{
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) //这个是什么异常,什么时候触发
{
}
}
}
}
{
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) //这个是什么异常,什么时候触发
{
}
}
}
}
他们不交互的.
是不是一个循环必须结束,才给启动另一个线程????
线程执行的过程是可能交叉的,并不是一个执行完了才执行另一个。
你可以在循环中打印Thread.currentThread().getName()看看输出结果。