曾经去一家公司笔试的题,简单的做出来了,可惜结果不理想
题目:启用两条多线程,一条线程实现变量 NO 加一,另一条实现变量 NO 减二。打印结果作者: 崔洪全 时间: 2012-11-18 08:42
class TestAdd implements Runnable
{
public void run(){
Test.testNumber = Test.testNumber+1;
System.out.println(Test.testNumber);
}
}
class TestDecrease implements Runnable
{
public void run(){
Test.testNumber=Test.testNumber-1;
System.out.println(Test.testNumber);
}
}
public class Test
{
public static int testNumber = 4;
public static void main(String[] args) throws Exception
{
TestAdd td = new TestAdd();
new Thread(td).start();
new Thread(td).start();
public class Test
{
public static int NO=123;
public static void main(String[] args) throws Exception
{
TestAdd td = new TestAdd();
TestDecrease tt = new TestDecrease();
new Thread(td).start();
new Thread(tt).start();
}
}