package LIU;public class T extends Thread{ private int num;
private int MAX = 50;
public T(int num)
{
this.num = num;
}
public void run()
{
synchronized(this)
{
while(this.num < 50)
{
System.out.println(this.num);
try
{
System.out.println(this.num);
this.wait();
System.out.println(this.num);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println(this.num);
this.num += 2;
this.notify();
}
}
static class Tj extends Thread
{
private int num;
private int MAX = 50;
public Tj(int num)
{
this.num = num;
}
public void run()
{
synchronized(this)
{
while(this.num < 50)
{
try
{
this.wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println(this.num);
this.num += 2;
this.notify();
}
}
}
public static void main(String[] args) {
(new Thread(new T(1))).start();
(new Thread(new Tj(0))).start();
}}
程序的大致意思就是利用线程控制交替输出奇数偶数,但是我的程序不知道哪里有问题,设置打印语句后发现wait后面的语句没有执行,我不知道是什么情况,应该怎么修改
private int MAX = 50;
public T(int num)
{
this.num = num;
}
public void run()
{
synchronized(this)
{
while(this.num < 50)
{
System.out.println(this.num);
try
{
System.out.println(this.num);
this.wait();
System.out.println(this.num);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println(this.num);
this.num += 2;
this.notify();
}
}
static class Tj extends Thread
{
private int num;
private int MAX = 50;
public Tj(int num)
{
this.num = num;
}
public void run()
{
synchronized(this)
{
while(this.num < 50)
{
try
{
this.wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println(this.num);
this.num += 2;
this.notify();
}
}
}
public static void main(String[] args) {
(new Thread(new T(1))).start();
(new Thread(new Tj(0))).start();
}}
程序的大致意思就是利用线程控制交替输出奇数偶数,但是我的程序不知道哪里有问题,设置打印语句后发现wait后面的语句没有执行,我不知道是什么情况,应该怎么修改
解决方案 »
- java正则表达式求助
- 一个有关QQ信息的问题
- JNDI 初学 NoInitialContextException
- 最后10分 问个正则表达式的问题
- 在JDK程序中怎么调用DLL文件
- Eclipse 3.0中输入中文为什么都变成方块了?要如何才能看到中文?!
- 请教JTable增加行后,如何让光标停在所增行的第一单元格问题
- 请问如何从标准输入输出设备中读取数据?
- [求助]关于线程的学习?
- 散分第四问:什么是接口?在JAVA API中有很多接口,为什么把这些定义为接口呢?在自已开发的程序中什么
- Java 执行顺序疑问
- 初学者请教接口问题:new线程对象时传入new Runnable()是多态吗?接口不是不能实例化吗?
1. 2个线程同时wait,就同时在等待,下面的根本没法输出,需要加一个flag来控制一个等待一个继续
2. 不需要写2个class, 一个共同的class PrintNumber就可以了。因为有共同点
3. 线程里面需要循环打印奇数和偶数。
4. 建议使用 ReentrantLock 配合 condition来写。/**
* this is the sample of print using 2 threads
*
*/
public final class ThreadPrintSample {
/** the flag represent is print the odd number first **/
private static volatile boolean isPrintOdd = true; /**
* static inner class using print the Odd number
*
*/
static class PrintNumber extends Thread {
private int num;
private Object instance; public PrintNumber(int num, Object instance) {
this.num = num;
this.instance = instance;
} public void run() {
synchronized (instance) {
final Thread current = Thread.currentThread();
final String threadName = current.getName(); while (true) {
if (current.isInterrupted()) {
System.out.println("the current thread isInterrupted,"
+ " thread name: " + threadName);
break;
} try {
if (isPrintOdd) {
isPrintOdd = false;
instance.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
} if (threadName.equalsIgnoreCase("Odd")) {
System.out.println("Print Odd Number, " + "number:"
+ this.num);
} else {
System.out.println("Print Even Number, number:"
+ this.num);
} this.num += 2;
instance.notify();
isPrintOdd = true; try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
}
} }
}
} /**
* the entrance of main program
*
* @param args
* @throws Throwable
*/
public static void main(String[] args) throws Throwable {
final ThreadPrintSample instance = new ThreadPrintSample();
final Thread t1 = new PrintNumber(1, instance);
final Thread t2 = new PrintNumber(0, instance); t1.setName("Odd");
t2.setName("Even"); t1.start();
t2.start(); t1.join();
t2.join(); System.out.println("over");
}
}
synchronized 同步的不是同一对象也是错误之一。 线程之间怎么传递消息?还有代码格式,看着变扭。