![](/img/trans.png)
[英]Java - print sequence of numbers using two threads with inter thread communication
[英]Print numbers 1-20 with two threads in Java
我正在嘗試用兩個線程打印數字 1-20:
我還有一個用於同步的鎖對象。
我的應用程序卡住了。 你能告訴我有什么問題嗎?
我的代碼:
public class runIt
{
public static void main(String[] args)
{
Odd odd = new Odd("odd thread");
Even even = new Even("even thread");
odd._t.start();
even._t.start();
try{
odd._t.join();
even._t.join();
}
catch (InterruptedException e){
System.out.println(e.getMessage());
}
}
}
public class Constants{
static Object lock = new Object();
}
public class Even implements Runnable{
Thread _t;
String _threadName;
public Even(String threadName){
_threadName = threadName;
_t = new Thread(this);
}
@Override
public void run(){
for (int i = 0; i < 20; i++){
if (i % 2 == 0){
synchronized (Constants.lock){
try{
Constants.lock.wait();
Constants.lock.notifyAll();
}
catch (InterruptedException e){
e.printStackTrace();
}
System.out.println(_threadName + " " + i + " ");
}
}
}
}
}
public class Odd implements Runnable{
Thread _t;
String _threadName;
public Odd(String threadName){
_threadName = threadName;
_t = new Thread(this);
}
@Override
public void run(){
for (int i = 0; i < 20; i++){
if (i % 2 == 1){
synchronized (Constants.lock){
try{
Constants.lock.wait();
Constants.lock.notifyAll();
}
catch (InterruptedException e1){
e1.printStackTrace();
}
System.out.println(_threadName + " " + i + " ");
}
}
}
}
}
我的輸出應該是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
謝謝你的幫助,譚。
您通過在synchronized
使用的對象上立即調用 wait 來濫用synchronized
和wait
,而無需在循環中的 synchronized 塊內進行檢查。 永遠不要再這樣做了。
事實上,這就是發生的事情:
synchronized
線上,您會鎖定Constants.lock
wait
行,您釋放對Constants.lock
的鎖並等待來自另一個線程的通知。那么你的編中發生了什么:
synchronized
並繼續阻塞第二個synchronized
因為第一個釋放了鎖您可以在 package declaraion 中提到的站點中找到解釋:這是工作代碼:
public class MultipleThreading {
int count = 1;
int MAX = 20;
public void printOdd() {
synchronized (this) {
while (count < MAX) {
while (count % 2 == 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(count + " ");
count++;
notify();
}
}
}
public void printEven() {
synchronized (this) {
while (count < MAX) {
while (count % 2 == 1) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(count + " ");
count++;
notify();
}
}
}
public static void main(String[] args) {
MultipleThreading mt = new MultipleThreading();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
mt.printEven();
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
mt.printOdd();
}
});
t1.start();
t2.start();
}
}
下面的代碼將幫助某人,
public class MyClass {
private static Object lock = new Object();
public static void main(String args[]){
Runnable runnable1 = new Runnable() {
@Override
public void run() {
for(int i=1; i<20; i=i+2){
synchronized (lock) {
System.out.println("Thread 1: "+i);
try {
lock.notifyAll();
lock.wait();
} catch (InterruptedException e) {
System.out.println("Error in Thread 1: "+e.getMessage());
}
}
}
}
};
Runnable runnable2 = new Runnable() {
@Override
public void run() {
for(int i=2; i<=20; i=i+2){
synchronized (lock) {
System.out.println("Thread 2: "+i);
try {
lock.notifyAll();
lock.wait();
} catch (InterruptedException e) {
System.out.println("Error in Thread 2: "+e.getMessage());
}
}
}
}
};
Thread thread1 = new Thread(runnable1);
Thread thread2 = new Thread(runnable2);
System.out.println("Thread Start: ");
thread1.start();
thread2.start();
}
}
Stack Overflow 上的每個人都嘗試了相同的解決方案。 簽出相同的不同實現。
public class PrintSequenceUsingTwo {
public static void main(String[] args) {
ThreadSequence sequence = new ThreadSequence();
Thread t1 = new Thread(()-> {try {
sequence.print();
} catch (InterruptedException e) {
e.printStackTrace();
}},"t1");
Thread t2 = new Thread(()-> {try {
sequence.print();
} catch (InterruptedException e) {
e.printStackTrace();
}},"t2");
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ThreadSequence {
private static int var = 0;
private int limit = 10; //set the variable value upto which you want to print
public synchronized void print() throws InterruptedException {
while (var<limit) {
notify();
System.out.println("Current Thread "+Thread.currentThread().getName()+" Value : "+(++var));
wait();
}
notify();
}
}
public class Print1To20 {
int couter=0;
static int N;
public void preven(){
synchronized (this) {
while(couter<N) {
while(couter%2==0) {
try {
wait();
} catch(Exception e) {
}
System.out.println(Thread.currentThread().getName()+":"+couter);
}
couter++;
notify();
}
}
}
public void prodd(){
synchronized (this) {
while(couter<N) {
while(couter%2==1) {
try {
wait();
} catch(Exception e) {
}
System.out.println(Thread.currentThread().getName()+":"+couter);
}
couter++;
notify();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// Thread t1= new Th;
//Thread t2= new Thread(new PrintOdd());
N=20;
Print1To20 pt= new Print1To20();
Thread t1= new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
pt.preven();
}
});
Thread t2= new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
pt.prodd();
}
});
t1.start();
t2.start();
}
}
參考:參考來自 GeekforGeeks
Try this solution.....
public class Print1To20Using2Thread {
public static void main(String[] args) {
PrintNumber pn = new PrintNumber(new Object());
Thread t1 = new Thread(pn, "First");
Thread t2 = new Thread(pn, "Second");
t1.start();
t2.start();
}
}
class PrintNumber implements Runnable {
Object lock;
int i = 0;
public PrintNumber(Object lock) {
this.lock = lock;
}
@Override
public void run() {
synchronized (lock) {
for (; i <= 20; i++) {
if (i == 11) {
try {
lock.wait(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(" "+Thread.currentThread().getName() + " " + i);
lock.notifyAll();
}
System.out.println();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.