实现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.

求大家指教 ,谢谢

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();
			}
		}
	}
}

 

把你的完整代码贴出来,包括你的包引用

看了一下,有几个错误,main函数里面有中文的句号,SubThread类里面的构造函数写成了AddThread构造函数,修改后代码如下:

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();
			}
		}
	}
}

 

不太清楚你本地的代码什么样,贴出来的这段有编译错误,我修改了下可以正常运行

package com.chrisf;

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();
			}
		}
	}
}



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+=x) {
            try {
                this.resource.add();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
class SubThread implements Runnable{
    private Resource resource;
    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();
            }
        }
    }
}

已修改,这个能运行的。有帮助的话点个关注Thanks♪(・ω・)ノ

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y