java 同步

s104892992 2008-10-20 12:00:11
用java同步实现:有四个进程,其中两个是实现j++,另外两个是j--,求代码
...全文
123 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jourqel 2008-10-20
  • 打赏
  • 举报
回复
lz可以考虑用锁机制~
lybjust 2008-10-20
  • 打赏
  • 举报
回复
所得是嘞
meadking 2008-10-20
  • 打赏
  • 举报
回复

class ThreadTest implements Runnable {
public void run() {
System.out.println ("someting run here");
}
public static void main (String[] args) {
ThreadTest tt = new ThreadTest();
Thread t1 = new Thread(tt);
Thread t2 = new Thread(tt);
t1.start();
t2.start();
//new Thread(tt).start();
}
}
nlq_84 2008-10-20
  • 打赏
  • 举报
回复


public class CsdnThread {
public static void main(String []args){
Arithmetic ari=new Arithmetic(100);
Thread []ariThread=new Thread[4];

//定义加的线程
ariThread[0]=new Thread(new Plus(ari,1,50));
ariThread[1]=new Thread(new Plus(ari,1,50));

//定义减的线程
ariThread[2]=new Thread(new Plus(ari,0,10));
ariThread[3]=new Thread(new Plus(ari,0,40));

//启动线程
ariThread[0].start();
ariThread[1].start();
ariThread[2].start();
ariThread[3].start();

// 等待线程结束
for(int i=0;i<ariThread.length;i++){
try {
ariThread[i].join();
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
System.out.println("运算结束,最后的结果为==》"+ari.getJ());
}
}


/**
*
* @author xiao
*
*/
class Plus implements Runnable{
Arithmetic ari=null;
int op;
int val;
/**
*
* @param ari 进行运算的对象
* @param op 运算符 1 为加,0为减
* @param val 要进行运算的值
*/
public Plus(Arithmetic ari,int op,int val){
this.ari=ari;
this.op=op;
this.val=val;
}

public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
System.out.println("当前的值为:"+ari.setNumber(op, val));
}

}


/**
* 进行运算的类
* @author xiao
*
*/
class Arithmetic{
private int j=0;

public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}

public Arithmetic(int j){
this.j=j;
}
/**
* @param op 运算方式 1表示 + 0表示 -
* @param k 要改变的值
* @return
*/
public synchronized int setNumber(int op,int k){

switch (op) {
case 1:
setJ(getJ()+k);
System.out.println("执行加法 k="+k);
return getJ()+k;
case 0:
setJ(getJ()-k);
System.out.println("执行减法");
return getJ()-k;
default:
System.out.println("没有合适的计算方法");
return 0;
}
}
}
meadking 2008-10-20
  • 打赏
  • 举报
回复
class ThreadTest extends Thread{
static int J=0;
public void run() {
System.out.println ("someting run here!");
J++;
}
public static void main (String[] args) {
ThreadTest tt = new ThreadTest();
tt.start();
}
}
nlq_84 2008-10-20
  • 打赏
  • 举报
回复

public class CsdnThread {
public static void main(String []args){
Arithmetic ari=new Arithmetic(100);
Thread []ariThread=new Thread[4];

//定义加的线程
ariThread[0]=new Thread(new Plus(ari,1,50));
ariThread[1]=new Thread(new Plus(ari,1,50));

//定义减的线程
ariThread[2]=new Thread(new Plus(ari,0,10));
ariThread[3]=new Thread(new Plus(ari,0,40));

//启动线程
ariThread[0].start();
ariThread[1].start();
ariThread[2].start();
ariThread[3].start();

// 等待线程结束
for(int i=0;i<ariThread.length;i++){
try {
ariThread[i].join();
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
System.out.println("运算结束,最后的结果为==》"+ari.getJ());
}
}


/**
*
* @author xiao
*
*/
class Plus implements Runnable{
Arithmetic ari=null;
int op;
int val;
/**
*
* @param ari 进行运算的对象
* @param op 运算符 1 为加,0为减
* @param val 要进行运算的值
*/
public Plus(Arithmetic ari,int op,int val){
this.ari=ari;
this.op=op;
this.val=val;
}

public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
System.out.println("当前的值为:"+ari.setNumber(op, val));
}

}


/**
* 进行运算的类
* @author xiao
*
*/
class Arithmetic{
private int j=0;

public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}

public Arithmetic(int j){
this.j=j;
}
/**
* @param op 运算方式 1表示 + 0表示 -
* @param k 要改变的值
* @return
*/
public synchronized int setNumber(int op,int k){

switch (op) {
case 1:
setJ(getJ()+k);
System.out.println("执行加法 k="+k);
return getJ()+k;
case 0:
setJ(getJ()-k);
System.out.println("执行减法");
return getJ()-k;
default:
System.out.println("没有合适的计算方法");
return 0;
}
}
}
Dragon_sxw 2008-10-20
  • 打赏
  • 举报
回复
网上多的是, 随便找份java笔试题差不多都包含这道题
sagezk 2008-10-20
  • 打赏
  • 举报
回复
[Quote=引用楼主 s104892992 的帖子:]
用java同步实现:有四个线程,其中两个是实现j++,另外两个是j--,求代码
[/Quote]

62,614

社区成员

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

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