关于线程的一道题,新人求讲解!感谢!

EsAcerThrea . 2019-06-17 05:48:33
题目要求:设计4个线程对象,两个线程执行加操作,两个执行减操作。
我想问下大家,我这么写有问题吗?然后我想实现的效果是,一加一减,交替进行。不知道该如何修改了,希望大家帮帮忙!谢谢!
以下代码:

public class 线程练习2 {
public static void main(String args[]){
Number nu=new Number();
Thread t1=new Thread(new AddNum(nu));
Thread t2=new Thread(new AddNum(nu));
Thread t3=new Thread(new SubNum(nu));
Thread t4=new Thread(new SubNum(nu));
t1.start();
t3.start();
t2.start();
t4.start();
}
}
class AddNum implements Runnable{
// private boolean flag=false;
private Number nu=null;
public AddNum(Number nu){
this.nu=nu;
}
@Override
public void run() {
int numNow = this.nu.getNum() + 1;
this.nu.setNum(numNow);
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class SubNum implements Runnable{
private Number nu=null;
public SubNum(Number nu){
this.nu=nu;
}
@Override
public void run() {
int numNow=this.nu.getNum()-1;
this.nu.setNum(numNow);
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

class Number{
private int num=10;
public synchronized void setNum(int num){
this.num=num;
System.out.println("Now,the number is: "+getNum());
}
public int getNum(){
return num;
}
}
...全文
1496 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
EsAcerThrea . 2019-06-30
  • 打赏
  • 举报
回复
引用 8 楼 djeclipse 的回复:
package com.thread; public class JoinTest { public static void main(String []args) { Number number = new Number(10); Object addLock = new Object(); Object subLock = new Object(); Thread thread1 = new Thread(new AddThread(number,addLock,subLock),"thread1"); Thread thread2 = new Thread(new AddThread(number,addLock,subLock),"thread2"); Thread thread3 = new Thread(new SubThread(number,subLock),"thread3"); Thread thread4 = new Thread(new SubThread(number,subLock),"thread4"); thread1.start(); thread2.start(); thread3.start(); thread4.start(); } } public class AddThread implements Runnable{ private Number number; private Object addLock; private Object subLock; public AddThread(Number number,Object addLock,Object subLock) { this.number = number; this.addLock = addLock; this.subLock = subLock; } @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized (addLock) { synchronized (subLock) { int num = number.getNum(); num ++; number.setNum(num); System.out.println("after addThread the num is:"+number.getNum()); subLock.notify(); } } } } public class SubThread implements Runnable{ private Number number; private Object subLock; public SubThread(Number number,Object subLock) { this.number = number; this.subLock = subLock; } @Override public void run() { synchronized (subLock) { try { subLock.wait(); int num = number.getNum(); num --; number.setNum(num); System.out.println("after subThread the num is:"+number.getNum()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public class Number { private int num; public Number(int num) { this.num = num; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } } 这样是可以交替执行的,当然除了这个还可以用join的方式来控制线程执行的先后顺序
谢谢谢谢 太有用了这个
EsAcerThrea . 2019-06-30
  • 打赏
  • 举报
回复
引用 7 楼 VECTOR WeiXiao 的回复:
和线程有关的就不仅仅是加flag了,加上lock或者sycronized去解决
嗯嗯 谢谢
djeclipse 2019-06-21
  • 打赏
  • 举报
回复
package com.thread;

public class JoinTest {

public static void main(String []args) {

Number number = new Number(10);
Object addLock = new Object();
Object subLock = new Object();
Thread thread1 = new Thread(new AddThread(number,addLock,subLock),"thread1");
Thread thread2 = new Thread(new AddThread(number,addLock,subLock),"thread2");
Thread thread3 = new Thread(new SubThread(number,subLock),"thread3");
Thread thread4 = new Thread(new SubThread(number,subLock),"thread4");

thread1.start();
thread2.start();
thread3.start();
thread4.start();
}

}


public class AddThread implements Runnable{

private Number number;
private Object addLock;
private Object subLock;

public AddThread(Number number,Object addLock,Object subLock) {
this.number = number;
this.addLock = addLock;
this.subLock = subLock;
}

@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (addLock) {
synchronized (subLock) {
int num = number.getNum();
num ++;
number.setNum(num);

System.out.println("after addThread the num is:"+number.getNum());
subLock.notify();
}
}

}

}

public class SubThread implements Runnable{

private Number number;

private Object subLock;

public SubThread(Number number,Object subLock) {
this.number = number;
this.subLock = subLock;
}

@Override
public void run() {

synchronized (subLock) {

try {
subLock.wait();
int num = number.getNum();
num --;
number.setNum(num);

System.out.println("after subThread the num is:"+number.getNum());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

}


public class Number {

private int num;

public Number(int num) {
this.num = num;
}

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}



}


这样是可以交替执行的,当然除了这个还可以用join的方式来控制线程执行的先后顺序
VECTOR WeiXiao 2019-06-20
  • 打赏
  • 举报
回复
和线程有关的就不仅仅是加flag了,加上lock或者sycronized去解决
qq_39936465 2019-06-19
  • 打赏
  • 举报
回复
引用 4 楼 EsAcerThrea . 的回复:
请问那是需要设置flag吗


是的没错

EsAcerThrea . 2019-06-19
  • 打赏
  • 举报
回复
引用 5 楼 qq_39936465 的回复:
[quote=引用 4 楼 EsAcerThrea . 的回复:] 请问那是需要设置flag吗
是的没错 [/quote]好我再试试感谢您
qq_39936465 2019-06-18
  • 打赏
  • 举报
回复
引用 楼主 EsAcerThrea . 的回复:
题目要求:设计4个线程对象,两个线程执行加操作,两个执行减操作。
我想问下大家,我这么写有问题吗?然后我想实现的效果是,一加一减,交替进行。不知道该如何修改了,希望大家帮帮忙!谢谢!


这就和生产消费一样,需要加同步锁,加的时候减不能实行,加完后释放锁同时禁止加,然后进行减,就这样循环。
EsAcerThrea . 2019-06-18
  • 打赏
  • 举报
回复
引用 3 楼 qq_39936465 的回复:
[quote=引用 楼主 EsAcerThrea . 的回复:] 题目要求:设计4个线程对象,两个线程执行加操作,两个执行减操作。 我想问下大家,我这么写有问题吗?然后我想实现的效果是,一加一减,交替进行。不知道该如何修改了,希望大家帮帮忙!谢谢!
这就和生产消费一样,需要加同步锁,加的时候减不能实行,加完后释放锁同时禁止加,然后进行减,就这样循环。[/quote]请问那是需要设置flag吗
EsAcerThrea . 2019-06-17
  • 打赏
  • 举报
回复
引用 1 楼 AlanZhang996 的回复:
问题太多了...
呃。。请教!
AlanZhang996 2019-06-17
  • 打赏
  • 举报
回复
问题太多了...

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧