这是一个关于进程同步通信的问题,编译的时候提醒我是未解决编译的问题,请大家看看

FashionCodeBoy 2016-10-26 09:55:58
package 11;



//两个线程一个打印a-z,一个线程打印A-Z

public class ThreadPrint {

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Object obj=new Object();

//新建实例,并启动线程
Thread1 t1=new Thread1(obj);
Thread2 t2=new Thread2(obj);
t1.start();
t2.start();
}


//线程第一步
class Thread1 extends Thread
{
//新建一个代码块进行同步
private Object obj;
public Thread1(Object obj)
{
this.obj=obj;
}
//线程第二步
//重写run()方法
public void run()
{
//要进行同步代码块

synchronized(obj)
{

for(int i=0;i<26;i++)
{
System.out.println((char)('a'+i)+" ");
obj.notifyAll();
try
{
if(i!=25)
{
//当线程在活动之前或活动期间处于正在等待、休眠或占用状态且该线程被中断时,抛出该异常
obj.wait();
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}




//创立第二个线程
class Thread2 extends Thread
{
private Object obj;
public Thread2(Object obj)
{
this.obj=obj;
}
public void run()
{
synchronized(obj)
{
for(int i=0;i<26;i++)
{
System.out.println((char)('A'+i)+" ");
obj.notifyAll();
try
{
if(i!=25)
{
obj.wait();
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}

}
}
}

}




发现进程没有同步,无法运行。
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type ThreadPrint is accessible. Must qualify the allocation with an enclosing instance of type ThreadPrint (e.g. x.new A() where x is an instance of ThreadPrint).
at 农金圈.ThreadPrint.main(ThreadPrint.java:14)
...全文
108 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
bichir 2016-10-26
  • 打赏
  • 举报
回复
你写成内部类去了,把thread1,2这两上clss放到threadprint的外面来或改main中thread的声明。
从小就很呆 2016-10-26
  • 打赏
  • 举报
回复
package com.jrj.test;
//两个线程一个打印a-z,一个线程打印A-Z
 
public class ThreadPrint {
 
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Object obj=new Object();
     
        //新建实例,并启动线程
        Thread1 t1=new ThreadPrint().new Thread1(obj);
        Thread2 t2=new ThreadPrint().new Thread2(obj);
        t1.start();
        t2.start();
    }
 
     
    //线程第一步
 class Thread1 extends Thread
    {
        //新建一个代码块进行同步
    private Object obj;
    public Thread1(Object obj)
    {
        this.obj=obj;
    }
        //线程第二步
        //重写run()方法
        public void run()
        {
            //要进行同步代码块
 
            synchronized(obj)
            {
                 
                for(int i=0;i<26;i++)
                {
                    System.out.println((char)('a'+i)+" ");
                    obj.notifyAll();
                      try
                        {
                            if(i!=25)
                            {
                                //当线程在活动之前或活动期间处于正在等待、休眠或占用状态且该线程被中断时,抛出该异常
                            obj.wait();
                            }
                        }
                        catch(InterruptedException e)
                        {
                            e.printStackTrace();
                        }
                    }
                }
            }
      }   
         
     
     
     
    //创立第二个线程
    class Thread2 extends Thread
    {
        private Object obj;
        public Thread2(Object obj) 
        {
            this.obj=obj;
        }
        public void run()
        {
            synchronized(obj)
            {
            for(int i=0;i<26;i++)
            {
                System.out.println((char)('A'+i)+" ");
                obj.notifyAll();   
                    try
                    {
                        if(i!=25)
                        {
                        obj.wait();
                        }
                    }
                    catch(InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
                 
            }
        }
     }
     
}
      
实例化内部类 需要通过当前类的实例化去调用内部类的实例方法。

62,629

社区成员

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

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