这是代码:
public class Test
{
public static void main(String[] args)
{
ReadThread read = new ReadThread("Read");
WriteThread write = new WriteThread("Write");
// 启动线程
read.start();
write.start();
}
}// 公共数据
class MyData
{
public static int data = 0;
public static boolean hasData = false;
}// 读线程
class ReadThread extends Thread
{
public ReadThread(String str)
{
super(str);
} public void run()
{
for (int i = 0; i < 10; i++)
{
this.get();
}
} public synchronized void get()
{
while (!MyData.hasData)
{
try
{
wait();
}
catch (InterruptedException e) { }
}
System.out.println(" 读取的数据为:" + MyData.data);
MyData.hasData = false;
notifyAll();
}
}// 写线程
class WriteThread extends Thread
{
public WriteThread(String str)
{
super(str);
} public void run()
{
for (int i = 1; i < 10; i++)
{
this.put(i);
}
} public synchronized void put(int newData)
{
while (MyData.hasData)
{
try
{
wait();
}
catch (InterruptedException e) { }
}
MyData.data = newData;
MyData.hasData = true;
System.out.println("写入的数据为:" + newData);
notifyAll();
}
}这是输出的结果:
写入的数据为:1
我不知道为什么会这样,这个问题我找了好久,还是没有找出来,请大家帮我看看,谢谢了!!
public class Test
{
public static void main(String[] args)
{
ReadThread read = new ReadThread("Read");
WriteThread write = new WriteThread("Write");
// 启动线程
read.start();
write.start();
}
}// 公共数据
class MyData
{
public static int data = 0;
public static boolean hasData = false;
}// 读线程
class ReadThread extends Thread
{
public ReadThread(String str)
{
super(str);
} public void run()
{
for (int i = 0; i < 10; i++)
{
this.get();
}
} public synchronized void get()
{
while (!MyData.hasData)
{
try
{
wait();
}
catch (InterruptedException e) { }
}
System.out.println(" 读取的数据为:" + MyData.data);
MyData.hasData = false;
notifyAll();
}
}// 写线程
class WriteThread extends Thread
{
public WriteThread(String str)
{
super(str);
} public void run()
{
for (int i = 1; i < 10; i++)
{
this.put(i);
}
} public synchronized void put(int newData)
{
while (MyData.hasData)
{
try
{
wait();
}
catch (InterruptedException e) { }
}
MyData.data = newData;
MyData.hasData = true;
System.out.println("写入的数据为:" + newData);
notifyAll();
}
}这是输出的结果:
写入的数据为:1
我不知道为什么会这样,这个问题我找了好久,还是没有找出来,请大家帮我看看,谢谢了!!
解决方案 »
- 下面这段代码是从书上抄的,可是为什么在执行时,提示下面的错误,求解决
- 请帮忙看一下这个程序,是怎样调用compareTo()方法的
- 一些基础的问题,不懂啊,请教一下.
- 同舟共济:请问这个程序内部的流程是怎么样的??(就是说程序怎么一步一步走的)
- 我是新手,大家看看这个出错信息是什么意思?
- 关于split方法的一个疑问
- static和 final 的顺序问题
- 线程start()与join()执行顺序问题
- 关于有状态session bean的问题,50分!
- lichlong(传龙),jant在哪下载?
- java生产excel表格问题,我想问可不可以一次生成多个excel
- NullPointerException 的问题
public class Test {
public static void main(String[] args) {
MyData myData=new MyData();
ReadThread read = new ReadThread("Read",myData);
WriteThread write = new WriteThread("Write",myData);
// 启动线程
read.start();
write.start();
}
}// 公共数据
class MyData {
public static int data = 0;
public static boolean hasData = false;
public synchronized void put(int newData) {
while (MyData.hasData) {
try {
wait();
} catch (InterruptedException e) {
}
}
MyData.data = newData;
MyData.hasData = true;
System.out.println("写入的数据为:" + newData);
notifyAll();
}
public synchronized void get() {
while (!MyData.hasData) {
try {
wait();
} catch (InterruptedException e) {
}
}
System.out.println(" 读取的数据为:" + MyData.data);
MyData.hasData = false;
notifyAll();
}
}// 读线程
class ReadThread extends Thread {
private MyData myData;
public ReadThread(String str,MyData myData) {
super(str);
this.myData=myData;
}
public void run() {
for (int i = 0; i < 10; i++) {
myData.get();
}
}
}// 写线程
class WriteThread extends Thread {
private MyData myData;
public WriteThread(String str,MyData myData) {
super(str);
this.myData=myData;
} public void run() {
for (int i = 1; i < 10; i++) {
myData.put(i);
}
}
}同步的方法放错地方了
而非线程方法.线程1 -访问-> 对象方法
线程2 -访问-> 对象方法(线程1在使用)
线程2 -堵塞-
线程1 -释放-> 对象方法
线程2 -唤醒- 线程2 -访问-> 对象方法
线程1 -访问-> 对象方法(线程1在使用)
线程1 -堵塞-
线程2 -释放-> 对象方法
线程1 -唤醒-
.
.
.1个对象方法被访问N个线程,所以,需要同步的是对象方法.
public static void main(String[] args) {
MyData myData=new MyData();
ReadThread read = new ReadThread("Read",myData);
WriteThread write = new WriteThread("Write",myData);
// 启动线程
read.start();
write.start();
try {
Thread.sleep(1000);
read.stop();
write.stop();
read.join();
write.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}// 公共数据
class MyData {
public static int data = 0;
public static boolean hasData = false;
public synchronized void put(int newData) {
while (MyData.hasData) {
try {
wait();
} catch (InterruptedException e) {
}
}
MyData.data = newData;
MyData.hasData = true;
System.out.println("写入的数据为:" + newData);
notifyAll();
}
public synchronized void get() {
while (!MyData.hasData) {
try {
wait();
} catch (InterruptedException e) {
}
}
System.out.println(" 读取的数据为:" + MyData.data);
MyData.hasData = false;
notifyAll();
}
}// 读线程
class ReadThread extends Thread {
private MyData myData;
public ReadThread(String str,MyData myData) {
super(str);
this.myData=myData;
}
public void run() {
for (int i = 0; i < 10; i++) {
myData.get();
}
}
}// 写线程
class WriteThread extends Thread {
private MyData myData;
public WriteThread(String str,MyData myData) {
super(str);
this.myData=myData;
} public void run() {
for (int i = 1; i < 10; i++) {
myData.put(i);
}
}
}
猜测你的想法,代码修改如下:package occupycpu;public class Test {
public static void main(String[] args) {
ReadThread read = new ReadThread("Read");
WriteThread write = new WriteThread("Write");
read.setT(write);
write.setT(read);
// 启动线程
read.start();
write.start();
}
}// 公共数据
class MyData {
private static int data = 0;
private static boolean hasData = false; public synchronized static int getData() {
return data;
} public synchronized static void setData(int data) {
MyData.data = data;
} public synchronized static boolean isHasData() {
return hasData;
} public synchronized static void setHasData(boolean hasData) {
MyData.hasData = hasData;
}
}// 读线程
class ReadThread extends Thread {
private Thread t = null; public ReadThread(String str) {
super(str);
} public void run() {
for (int i = 0; i < 10; i++) {
this.get();
}
} public void get() {
while (!MyData.isHasData()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
System.out.println(" 读取的数据为:" + MyData.getData());
MyData.setHasData(false);
t.interrupt();
} public Thread getT() {
return t;
} public void setT(Thread t) {
this.t = t;
}
}// 写线程
class WriteThread extends Thread { private Thread t = null; public WriteThread(String str) {
super(str);
} public void run() {
for (int i = 1; i < 10; i++) {
this.put(i);
}
} public void put(int newData) {
while (MyData.isHasData()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
MyData.setData(newData);
MyData.setHasData(true);
System.out.println("写入的数据为:" + newData);
t.interrupt();
} public Thread getT() {
return t;
} public void setT(Thread t) {
this.t = t;
}
}仅供参考。
我靠,java code怎么发地,再试试。。public class Test { public static void main(String[] args) {
}
}
}
}
public class Test { public static void main(String[] args) {
}
}
简单(基本)(迷糊)类型,比如int,boolean不能用变量同步,只能用方法同步,方法同步也是对象同步,就是任何时刻,一个对象中只能有一个同步方法在执行,如果是其他的变量(类的实例等,怎么称呼来着?),可以用变量同步,很多情况下用变量同步就够了,变量同步只同步一小块需要的代码,这样效率会高一些。其他还有很多,这是基本的。仅供参考。