我必须使用两个线程,一个线程打印所有小于10的奇数,另一个打印小于10的偶数,最后的输出应该是顺序的。
我已达到以下目的。我想用同步方法做同样的事情?怎么做?
class printodd extends Thread{
public void run() {
super.run();
for(int i=0;i<10;i=i+2){
System.out.println("even "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class printeven extends Thread{
public void run() {
super.run();
for(int i=1;i<10;i=i+2)
{
System.out.println("odd "+i);
try {
Thread.sleep(1050);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class PrintNumSeq{
public static void main(String[] args) {
printodd p=new printodd();
printeven e=new printeven();
e.start();
p.start();
}
}
答案 0 :(得分:0)
试试这个
public class PrintNumSeq extends Thread {
static Object lock = new Object();
static int n;
int even;
PrintNumSeq(int r) {
this.even = r;
}
public void run() {
try {
synchronized (lock) {
for (;;) {
while ((n & 1) != even) {
lock.wait();
}
n++;
lock.notify();
if (n > 10) {
break;
}
System.out.println(n);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new PrintNumSeq(1).start();
new PrintNumSeq(0).start();
}
}
输出
1
2
3
4
5
6
7
8
9
10
答案 1 :(得分:0)
public class SequentialThreadPrinter {
public static void main(String[] args) {
AtomicInteger counter = new AtomicInteger(0);
EvenThread even = new EvenThread("even", counter);
OddThread odd = new OddThread("odd", counter);
even.start();
odd.start();
}
}
private static class EvenThread extends Thread {
private String name;
private AtomicInteger counter;
public EvenThread(String name, AtomicInteger counter) {
this.name = name;
this.counter = counter;
}
public void run() {
do {
synchronized (counter) {
if (counter.get() % 2 == 0) {
System.out.println("Thread is " + name + ", Counter is = " + counter.getAndAdd(1));
counter.notifyAll();
} else {
try {
counter.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} while (counter.get() <= 10);
}
}
private static class OddThread extends Thread {
private String name;
private AtomicInteger counter;
public OddThread(String name, AtomicInteger counter) {
this.name = name;
this.counter = counter;
}
public void run() {
do {
synchronized (counter) {
if (counter.get() % 2 != 0) {
System.out.println("Thread is " + name + ", Counter is = " + counter.getAndAdd(1));
counter.notifyAll();
} else {
try {
counter.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} while (counter.get() <= 10);
}
}
}
答案 2 :(得分:-1)
您好,在这里您必须使用java同步。基本上,同步是线程之间共享的Java机制,它将在运行时阻止所有其他线程。通过这种方式,您可以按顺序打印它们。 您可以阅读以下教程来理解它
http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html
使用它时要小心,因为不小心使用可能会造成死锁 http://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html
答案 3 :(得分:-2)
你可以通过让线程获得一个公共锁来实现这一点,以便允许打印任何东西。
&#34;锁定&#34;可能是一些单身人士:
public class Lock {
private static Lock instance;
private static boolean inUse = false;
public static Lock getInstance() {
if(instance == null) {
instance = new Lock();
}
return instance;
}
public boolean acquireLock() {
boolean rv = false;
if(inUse == false) {
inUse = true;
rv = true;
}
return rv;
}
public void releaseLock() {
inUse = false;
}
}
每当线程想要打印时,它必须调用acquireLock()
并且如果它返回true,则它可以打印。如果返回false,则必须等到它返回true。打印后,线程立即调用releaseLock()
,以便释放锁定。
我没有测试此代码,因此使用它需要您自担风险。我只是快速输入它,因为这是我想到的想法。
您可以在此处阅读有关锁及其同步使用的更多信息:http://en.wikipedia.org/wiki/Lock_(computer_science)