经典指数          
原因
1003
浏览数
0
收藏数
 

public class NameList
{
    private List names = new ArrayList();
    public synchronized void add(String name)
    {
        names.add(name);
    }
    public synchronized void printAll()     {
        for (int i = 0; i < names.size(); i++)
        {
            System.out.print(names.get(i) + ””);
        }
    }

    public static void main(String[]args)
    {
        final NameList sl = new NameList();
        for (int i = 0; i < 2; i++)
        {
            new Thread()
            {
                public void run()
                {
                    sl.add(“A”);
                    sl.add(“B”);
                    sl.add(“C”);
                    sl.printAll();
                }
            } .start();
        }
    }
}
Which two statements are true if this class is compiled and run?

  • An exception may be thrown at runtime.
  • The code may run with no output, without exiting.
  • The code may run with no output, exiting normally(正常地).
  • The code may rum with output “A B A B C C “, then exit.
  • The code may rum with output “A B C A B C A B C “, then exit.
  • The code may ruin with output “A A A B C A B C C “, then exit.
  • The code may ruin with output “A B C A A B C A B C “, then exit.

     举报   纠错  
 
切换
暂时还没有答案,欢迎分享你的解答 . . .
撰写答案
扫描后移动端查看本题