![]() |
#2
林月儿2018-06-17 21:55
|

package 多线程;
class Thread1 implements Runnable
{
public synchronized void run()
{
try
{
for(int i=0;i<10;i++)
{
if((i-1)!=0&&(i-1)%2==0)
{
wait();
}
System.out.println(i);
}
}
catch(InterrupetedException e1)
{
e1.printStackTrace();
}
}
}
class Thread2 implements Runnable
{
public synchronized void run()
{ try
{
for(int i=66;i<70;i++)
{
if(i!=66&&i%2==0)
{
wait();
notify();
}
System.out.println((char)i);
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
public class MyThread
{
public static void main(String args[])
{
Thread1 hello=new Thread1();
Thread2 nihao=new Thread2();
Thread thread1,thread2;
thread1= new Thread(hello);
thread2= new Thread(nihao);
thread1.start();
thread2.start();
}
}
class Thread1 implements Runnable
{
public synchronized void run()
{
try
{
for(int i=0;i<10;i++)
{
if((i-1)!=0&&(i-1)%2==0)
{
wait();
}
System.out.println(i);
}
}
catch(InterrupetedException e1)
{
e1.printStackTrace();
}
}
}
class Thread2 implements Runnable
{
public synchronized void run()
{ try
{
for(int i=66;i<70;i++)
{
if(i!=66&&i%2==0)
{
wait();
notify();
}
System.out.println((char)i);
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
public class MyThread
{
public static void main(String args[])
{
Thread1 hello=new Thread1();
Thread2 nihao=new Thread2();
Thread thread1,thread2;
thread1= new Thread(hello);
thread2= new Thread(nihao);
thread1.start();
thread2.start();
}
}
目标输出12ab34cd56ef78gh这样子的
但是结果是
Exception in thread "Thread-0" B
C
java.lang.Error: 无法解析的编译问题:
InterrupetedException 无法解析为类型
at 多线程.Thread1.run(MyThread.java:17)
at java.base/java.lang.Thread.run(Thread.java:844)
来个大神帮下忙勒,多谢啦!!!!