黑马程序员技术交流社区
标题:
关于一个线程安全的问题
[打印本页]
作者:
再见亦是泪
时间:
2013-1-18 20:05
标题:
关于一个线程安全的问题
package ThreadLearn;
class Res
{
static String Name = "李斯";
static String Sex = "男";
static boolean flag = false;
static Object obj = new Object();
}
class Input implements Runnable
{
public void run()
{
for(int x=0; x < 10; x++)
{
synchronized(Res.obj)
{
if(Res.flag)
{
try{Res.obj.wait();}catch(Exception e){}
}
if(x%2 == 0)
{
Res.Name = "麦克。。。。";
Res.Sex = "M男。。。。。。。。。";
}
else
{
Res.Name = "Lily";
Res.Sex = "Female";
}
Res.flag = false;
Res.obj.notifyAll();
}
}
}
}
class Output implements Runnable
{
public void run()
{
synchronized(Res.obj)
{
for(int i = 0; i < 10 ; i++)
{
if(!Res.flag)
{
try{Res.obj.wait();}catch(Exception e){}
}
System.out.println(Res.Name+"....."+Res.Sex);
Res.flag = true;
Res.obj.notifyAll();
}
}
}
}
public class ThreadLearn3
{
public static void main(String args[])
{
Input input = new Input();
Output output = new Output();
Thread t1 = new Thread(input);
Thread t2 = new Thread(output);
t1.start();
t2.start();
}
}
作者:
再见亦是泪
时间:
2013-1-18 20:05
我要每个名字轮翻打一次,如这样写就只能一个名字打出来 不能交替 何解
作者:
txl
时间:
2013-1-18 20:23
本帖最后由 黑马唐贤来 于 2013-1-18 20:27 编辑
//同学你好,我修改你的代码后实现了你的要求
class Res
{
static String Name = "李斯";
static String Sex = "男";
static boolean flag = false;
static Object obj = new Object();
}
class Input implements Runnable
{
public void run()
{
for(int x=0; x < 10; x++)
{
synchronized(Res.obj)
{
//if(Res.flag)
while(Res.flag) //这里应该使用while(),这样可以使线程每次被唤醒后都会去检查Res.flag值
{
try{Res.obj.wait();}catch(Exception e){}
}
if(x%2 == 0)
{
Res.Name = "麦克。。。。";
Res.Sex = "M男。。。。。。。。。";
}
else
{
Res.Name = "Lily";
Res.Sex = "Female";
}
Res.flag = true; //这里修改为true,因为必须要让这个线程执行一遍后wait
Res.obj.notifyAll();
}
}
}
}
class Output implements Runnable
{
public void run()
{
synchronized(Res.obj)
{
for(int i = 0; i < 10 ; i++)
{
//if(!Res.flag)
while(!Res.flag) //这里使用(!Res.flag)取反来调节线程运行
{
try{Res.obj.wait();}catch(Exception e){}
}
System.out.println(Res.Name+"....."+Res.Sex);
Res.flag = false;
Res.obj.notifyAll();
}
}
}
}
public class Demo2
{
public static void main(String args[])
{
Input input = new Input();
Output output = new Output();
Thread t1 = new Thread(input);
Thread t2 = new Thread(output);
t1.start();
t2.start();
}
}
希望共同努力,加油
复制代码
作者:
高浩
时间:
2013-1-18 21:12
//同学你好,我修改你的代码后实现了你的要求
class Res
{
static String Name = "李斯";
static String Sex = "男";
static boolean flag = false;
static Object obj = new Object();
}
class Input implements Runnable
{
public void run()
{
for(int x=0; x < 10; x++)
{
synchronized(Res.obj)
{
//if(Res.flag)
while(Res.flag) //这里应该使用while(),这样可以使线程每次被唤醒后都会去检查Res.flag值
{
try{Res.obj.wait();}catch(Exception e){}
}
if(x%2 == 0)
{
Res.Name = "麦克。。。。";
Res.Sex = "M男。。。。。。。。。";
}
else
{
Res.Name = "Lily";
Res.Sex = "Female";
}
Res.flag = true; //这里修改为true,因为必须要让这个线程执行一遍后wait
Res.obj.notifyAll();
}
}
}
}
class Output implements Runnable
{
public void run()
{
synchronized(Res.obj)
{
for(int i = 0; i < 10 ; i++)
{
//if(!Res.flag)
while(!Res.flag) //这里使用(!Res.flag)取反来调节线程运行
{
try{Res.obj.wait();}catch(Exception e){}
}
System.out.println(Res.Name+"....."+Res.Sex);
Res.flag = false;
Res.obj.notifyAll();
}
}
}
}
public class Demo2
{
public static void main(String args[])
{
Input input = new Input();
Output output = new Output();
Thread t1 = new Thread(input);
Thread t2 = new Thread(output);
t1.start();
t2.start();
}
}
写while循环进行判断是因为线程等待后在此被唤醒会继续它上次的操作继续运行,这样会出现问题,
循环的在让这个线程在判断一次就不会出先错误了,
赋值的任务中没赋值完应该让标识该成true然后判断到true是该线程等待,让输出的线程来执行,输
出线程执行完后在将标识符该为false,在去唤醒等待的赋值线程,如此交替进行,就可以实现你想要
达到的目的了
作者:
再见亦是泪
时间:
2013-1-18 22:19
谢谢,共同努力
作者:
黄金龙
时间:
2013-1-19 11:42
class Res
{
private String name = "李斯";
private String Sex = "男";
private boolean flag = false;
public synchronized void set(String name , String Sex)
{
while(flag)
try{this.wait();}catch(Exception e){};
this.name = name;
this.Sex = Sex;
flag = true;
this.notify();
}
public synchronized void out ()
{
while(!flag)
try{this.wait();}catch(Exception e){};
System.out.println(name+"....."+Sex);
flag = false;
this.notify();
}
}
class Input implements Runnable
{
private Res r;
Input(Res r)
{
this.r=r;
}
public void run()
{
for(int x=0; x < 10; x++)
{
if(x%2 == 0)
r.set("麦克。。。。","M男。。。。。。。。。");
else
r.set("Lily","Female");
}
}
}
class Output implements Runnable
{
private Res r;
Output(Res r)
{
this.r=r;
}
public void run()
{
for(int i = 0; i < 10 ; i++)
r.out();
}
}
public class Demo08
{
public static void main(String args[])
{
Res r = new Res();
Input input = new Input(r);
Output output = new Output(r);
Thread t1 = new Thread(input);
Thread t2 = new Thread(output);
t1.start();
t2.start();
}
}
复制代码
th1111.jpg
(14.64 KB, 下载次数: 28)
下载附件
2013-1-19 11:42 上传
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2