您的位置:首页 > 其它

两个线程交替打印字符串

2014-04-11 11:02 344 查看
每个对象都有一内置锁

wait方法 释放对象锁(不占对象锁)

sleep方法不释放对象锁(占对象锁)

优秀写法 (下面写法可能有问题,synchronized (LOCK) 提到 while前面就好了)

class Info {
String printStr = "i think this is ok";
int i = 0;

public void print() {
if (i < printStr.length()) {
System.out.println(Thread.currentThread().getName() + "  print   "
+ printStr.charAt(i));
i++;
}
}
}

public class main {
private static Object LOCK = new Object();
static Info info = new Info(); // 互斥资源
static boolean flag = false; // false for a ,true for b

public static void main(String[] args) {
// 两个线程 交替打印字符串
Thread a = new Thread() {
public void run() {
while (info.i < info.printStr.length())
synchronized (LOCK) {
{
if (false == flag) {
try {
LOCK.wait();// 在wait后的瞬间线程b得到锁
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag = false;
info.print();
LOCK.notify();// 在这里虽然唤醒了另一个线程b,但锁并没有释放
}
}
};
};
Thread b = new Thread() {
public void run() {
while (info.i < info.printStr.length())
synchronized (LOCK) {
{
if (true == flag) {
try {
LOCK.wait();// 在wait后的瞬间线程b得到锁
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag = true;
info.print();
LOCK.notify();// 在这里虽然唤醒了另一个线程b,但锁并没有释放
}
}
};
};
a.start();
b.start();
}

}


代码1

package ThreadTest;

class Info
{
String printStr="i think this is ok";
int i=0;
boolean flag=false;
public  void print1()
{
synchronized(this)
{
if(flag==false)
{
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
realprint();
flag=false;
notify();
}
}
public  void print2()
{
synchronized(this)
{
if(flag==true)
{
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
realprint();
flag=true;
notify();
}
}
public void realprint()
{
if(i<printStr.length())
{
System.out.println(Thread.currentThread().getName() +"  print   "+printStr.charAt(i));
i++;
}
}
}
class MyThread1 extends Thread
{
public Info info=null;
public MyThread1(Info in) {
this.info=in;
}
@Override
public void run()
{
while(info.i<info.printStr.length())
info.print2();
}
}
class MyThread2 extends Thread
{
public Info info=null;
public MyThread2(Info in) {
this.info=in;
}
@Override
public void run()
{
while(info.i<info.printStr.length())
info.print1();
}
}
public class main {
public static void main(String[] args) {
//两个线程    交替打印字符串
Info info=new Info();                                    //互斥资源
MyThread1 mthread1=new MyThread1(info);
MyThread2 mthread2=new MyThread2(info);
new Thread(mthread1).start();
new Thread(mthread2).start();
}

}


代码2

package MyThreadMethod;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

class Info
{
String printStr="i think this is ok";
int i=0;
public  void print1()
{
if(i<printStr.length())
{
System.out.println(Thread.currentThread().getName() +"  print   "+printStr.charAt(i));
i++;
}
}
}
public class threadMethod {
ReentrantLock lock=new ReentrantLock();
Condition con1=lock.newCondition();
Condition con2=lock.newCondition();
boolean flag=false;
public class MyThread1 extends Thread
{
public Info info=null;
MyThread1(Info info)
{
this.info=info;
}
@Override
public void run() {
while(info.i<info.printStr.length())
{
lock.lock();
try
{
while(flag==true)
{
try {
con1.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
info.print1();
flag=true;
con2.signal();
}
finally
{
lock.unlock();
}
}
}
}

public  class MyThread2 extends Thread
{
public Info info=null;
MyThread2(Info info)
{
this.info=info;
}
@Override
public void run() {
while(info.i<info.printStr.length())
{
lock.lock();
try
{
while(flag==false)
{
try {
con2.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
info.print1();
flag=false;
con1.signal();
}
finally
{
lock.unlock();
}
}
}
}
public static void main(String[] args) {
threadMethod tm=new threadMethod();
Info info=new Info();
MyThread1 thread1=tm.new MyThread1(info);
MyThread2 thread2=tm.new MyThread2(info);
thread1.start();
thread2.start();

}

}


3 不同写法

class Info
{
String printStr="i think this is ok";
int i=0;
public void print()
{
if(i<printStr.length())
{
System.out.println(Thread.currentThread().getName() +"  print   "+printStr.charAt(i));
i++;
}
}
}
public class main {
private static Object LOCK = new Object();
static Info info=new Info();                                    //互斥资源
static boolean flag=false;  //false for a ,true for b
public static void main(String[] args) {
//两个线程    交替打印字符串
Thread a=new Thread(){
public void run() {
while(info.i<info.printStr.length())
synchronized (LOCK) {
{
if(false==flag)
{
flag=true;
info.print();
LOCK.notify();//在这里虽然唤醒了另一个线程b,但锁并没有释放
try {
if(info.i<info.printStr.length())
LOCK.wait();//在wait后的瞬间线程b得到锁
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
};
Thread b=new Thread(){
public void run() {
while(info.i<info.printStr.length())
synchronized (LOCK) {
{
if(true==flag)
{
flag=false;
info.print();
LOCK.notify();//在这里虽然唤醒了另一个线程b,但锁并没有释放
try {
if(info.i<info.printStr.length())
LOCK.wait();//在wait后的瞬间线程b得到锁
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
};
a.start();
b.start();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: