weixin_49641905 2021-05-03 09:15 采纳率: 0%
浏览 105

实现Runnable接口时出错

 

public class ProjectDemo {

	public static void main(String[] args) {
		Resource res = new Resource();
		AddThread at = new AddThread(res);
		SubThread st = new SubThread(res);
		new Thread(at, "加法线程-A")。start();
		new Thread(st, "减法线程-B")。start();
		new Thread(at, "加法线程-X")。start();
		new Thread(st, "减法线程-Y")。start();
	}

}

class Resource{		//定义一个操作的资源
	private int num;		//要进行加减操作的数据
	private boolean flag = true;	//加减的切换
	//flag:true 可以进行加操作,不能进行减操作
	//flag:false 可以进行减操作,不能进行加操作
	
	public synchronized void add() throws Exception{	//执行加法操作
		Thread.sleep(100);		//加入延迟便于观察问题
		this.num++;
		System.out.println("【加法操作" + Thread.currentThread().getName() + "】num = " + this.num);
	}
	public synchronized void sub() throws Exception{	//执行减法操作
		Thread.sleep(200);
		this.num--;
		System.out.println("【减法操作" + Thread.currentThread().getName() + "】num = " + this.num);
	}
}

class AddThread implements Runnable{
	private Resource resource;
	public AddThread(Resource resource) {
		this.resource = resource;
	}
	@Override
	public void run() {
		for(int x=0; x<50; x+=) {
			try {
				this.resource.add();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

class SubThread implements Runnable{
	private Resource resource;
	public AddThread(Resource resource) {
		this.resource = resource;
	}
	@Override
	public void run() {
		for(int x=0; x<50; x++) {
			try {
				this.resource.sub();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

用的是eclipse,提示说the type Runnable cannot be the superinterface of AddThread, a superinterface must be a interface.

求大家指教 ,谢谢

  • 写回答

7条回答 默认 最新

  • 关注
    public class ProjectDemo {
     
    	public static void main(String[] args) {
    		Resource res = new Resource();
    		AddThread at = new AddThread(res);
    		SubThread st = new SubThread(res);
    		new Thread(at, "加法线程-A").start();
    		new Thread(st, "减法线程-B").start();
    		new Thread(at, "加法线程-X").start();
    		new Thread(st, "减法线程-Y").start();
    	}
     
    }
     
    class Resource{		//定义一个操作的资源
    	private int num;		//要进行加减操作的数据
    	private boolean flag = true;	//加减的切换
    	//flag:true 可以进行加操作,不能进行减操作
    	//flag:false 可以进行减操作,不能进行加操作
    	
    	public synchronized void add() throws Exception{	//执行加法操作
    		Thread.sleep(100);		//加入延迟便于观察问题
    		this.num++;
    		System.out.println("【加法操作" + Thread.currentThread().getName() + "】num = " + this.num);
    	}
    	public synchronized void sub() throws Exception{	//执行减法操作
    		Thread.sleep(200);
    		this.num--;
    		System.out.println("【减法操作" + Thread.currentThread().getName() + "】num = " + this.num);
    	}
    }
     
    class AddThread implements Runnable{
    	private Resource resource;
    	public AddThread(Resource resource) {
    		this.resource = resource;
    	}
    	@Override
    	public void run() {
    		for(int x=0; x<50; x++) {
    			try {
    				this.resource.add();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
     
    class SubThread implements Runnable{
    	private Resource resource;
    	public SubThread(Resource resource) {
    		this.resource = resource;
    	}
    	@Override
    	public void run() {
    		for(int x=0; x<50; x++) {
    			try {
    				this.resource.sub();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮