解决方案 »
- 【热烈讨论】Hibernate中什么时候该用HQL什么时候该使用QBC【推荐】
- SWING界面布局问题
- 程序打成jar包后,如何使用log4j.properties文件?
- 关于Swing实现国际化
- JAVA操作系统编程
- 请问怎么实现在一个字符串中查找另一个子串的算法(不用String类提供的方法)
- 怎样将Frame上的各级面板和组件上的右键单击事件由Frame统一处理?
- 在Java中能够使用ActiveX控件吗
- 谁能提供一个SQL Server 7.0 的JDBC驱动阿,要FREE的
- 谢谢!请问如何建立JAVA小程序运行环境??
- 怎么访问JAR包下的文件,路径?
- 整型数组A,长度为200;再给定一个数组B,B内存放A中部分元素的下标,B内的元素无序;问题:对A中下标在B内的元素进行甲操作,其他元素进行乙操作
private Object prev;
private Object self;
private int count = 1;
public OddThread(Object prev, Object self) {
this.prev = prev;
this.self = self;
} @Override
public void run() {
int result = count * 2 - 1;
while (result < 50) {
synchronized (prev) {
synchronized (self) {
System.out.println(result);
count++;
self.notify();
result = count * 2 - 1;
}
if (result >= 50) {
break;
}
try {
prev.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }}package com.thread;public class EvenThread extends Thread {
private Object prev;
private Object self;
private int count = 1;
public EvenThread(Object prev, Object self) {
this.prev = prev;
this.self = self;
} @Override
public void run() {
int result = count * 2;
while (result <= 50) {
synchronized (prev) {
synchronized (self) {
System.out.println(result);
count++;
self.notify();
result = count * 2;
}
if (result > 50) {
break;
}
try {
prev.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }}package com.thread;public class NumberTest {
public static void main(String[] args) {
Object a = new Object();
Object b = new Object();
OddThread pa = new OddThread(b, a);
EvenThread pb = new EvenThread(a, b);
new Thread(pa).start();
new Thread(pb).start();
}
}这是我借鉴以前网上看到的类似问题修改后的代码,应该可以解决你的问题
你只需用个同步块就可以了:示例:public class NumberTest{
public static void main(String[] args) {
T1 t1 = new T1();
T2 t2 = new T2();
t2.start();
t1.start();
}
}
class T1 extends Thread { @Override
public void run() {
synchronized (NumberTest.class) {
for (int i = 1; i <= 50; i++) {
System.out.println(i * 2);
NumberTest.class.notify();
try {
NumberTest.class.wait(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}}class T2 extends Thread { @Override
public void run() {
synchronized (NumberTest.class) {
for (int i = 1; i <= 50; i++) {
System.out.println(i * 2 - 1);
NumberTest.class.notify();
try {
NumberTest.class.wait(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
{
static boolean isT1 = false;
public static void main(String[] args)
{
final T1 t1 = new T1();
final T2 t2 = new T2();
new Thread()
{
@Override
public void run()
{
for(int i = 1; i <= 50; i++)
{
t1.printf(i);
}
};
}.start();
new Thread()
{
@Override
public void run()
{
for(int i = 1; i <= 50; i++)
{
t2.printf(i);
}
};
}.start();
}
}
class T1
{
public void printf(int i)
{
synchronized (ThreadTest.class)
{
while(!ThreadTest.isT1)
{
try
{
ThreadTest.class.wait();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(i * 2);
ThreadTest.isT1 = false;
ThreadTest.class.notify();
}
}
}class T2
{
public void printf(int i)
{
synchronized (ThreadTest.class)
{
while(ThreadTest.isT1)
{
try
{
ThreadTest.class.wait();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(i * 2 - 1);
ThreadTest.isT1 = true;
ThreadTest.class.notify();
}
}
}
你只需用个同步块就可以了:示例:public class NumberTest{
public static void main(String[] args) {
T1 t1 = new T1();
T2 t2 = new T2();
t2.start();
t1.start();
}
}
class T1 extends Thread { @Override
public void run() {
synchronized (NumberTest.class) {
for (int i = 1; i <= 50; i++) {
System.out.println(i * 2);
NumberTest.class.notify();
try {
NumberTest.class.wait(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}}class T2 extends Thread { @Override
public void run() {
synchronized (NumberTest.class) {
for (int i = 1; i <= 50; i++) {
System.out.println(i * 2 - 1);
NumberTest.class.notify();
try {
NumberTest.class.wait(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
不好意思,你这段代码是有问题的,无法保证T1和T2的初始的先后执行顺序
你只需用个同步块就可以了:[/code]
由于wait方法会释放锁,无法保证数字打印的顺序
改正一下示例:public class NumberTest{
public static void main(String[] args) {
Number num = new Number();
T1 t1 = new T1(num);
T2 t2 = new T2(num);
t2.start();
t1.start();
}
}
class T1 extends Thread {
Number num;
T1 (Number num){
this.num = num;
}
@Override
public void run() {
while(num.getNum() < 100){
num.printEven();
}
}}class T2 extends Thread {
Number num;
T2 (Number num){
this.num = num;
}
@Override
public void run() {
while(num.getNum() < 100){
num.printOdd();
}
}
}class Number{
int i = 1;
public synchronized void printOdd(){
while(i % 2 != 1){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(i<=100)
System.out.println(i);
i++;
notifyAll();
}
public synchronized void printEven(){
while(i % 2 != 0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(i<=100)
System.out.println(i);
i++;
notifyAll();
}
public synchronized int getNum(){
return i;
}
}