标签:java 使用 io ar 2014 art 代码 sp on
/**
* @author laishengfeng
* @2014-8-27
* @TODO 编写一个程序, 四个线程,其中两个线程每次对j增加1,
* 另外两个线程对j每次减少1(要求使用内部类线程)
*/
public class Test
{
public static void main(String[] args) {
MyThread mt = new MyThread(); //MyThread对象
mt.new InnerThread1().start();
mt.new InnerThread1().start();
mt.new InnerThread2().start();
mt.new InnerThread2().start();
}
}
class MyThread
{
private int j = 100;
/*对j加的内部类*/
public class InnerThread1 extends Thread
{
public void run() {
while (j < 120)
{
try {
Thread.sleep(500);
}catch (Exception ex) {
}
j++;
System.out.println(Thread.currentThread().getName()+" "+j);
}
}
}
/*对j减的内部类*/
public class InnerThread2 extends Thread
{
public void run() {
while (j > 80)
{
try {
Thread.sleep(300);
}catch (Exception ex) {
}
j--;
System.out.println(Thread.currentThread().getName()+" "+j);
}
}
}
}
直接上代码 有兴趣的MM我
编写一个程序, 四个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1,要求使用内部类
标签:java 使用 io ar 2014 art 代码 sp on
原文地址:http://my.oschina.net/lsf930709/blog/307450