Quizzes
Site Language: English
Українська
English
Русский
Programming Tests
Login
Sign Up
Programming Tests
Theory
Snippets
Papers
Landing
Android
Prices
FAQ
Cosmo Story
Terms and Conditions
Privacy Policy
Cookies Policy
Send Feedback
Concurrent collections
:
Content language: English
Русский
Java Collections
Concurrent collections
What will the following code's execution print to console? import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; public class Test { private static void removeAndPrint(List<String> list) { for (String str : list) { if (str.equals("two")) { list.remove("three"); } } System.out.println(list); } public static void main(String[] args) { List<String> list = new CopyOnWriteArrayList<String>(); list.add("one"); list.add("two"); list.add("three"); list.add("four"); removeAndPrint(list); } }
Concurrent collections
What is the result of the code? import java.util.*; public class TestIterator{ public static void main(String[] args){ final List synchList = Collections.synchronizedList(new ArrayList()); fillList(synchList, 10); Thread anotherThread = new Thread(){ public void run(){ try{ Thread.sleep(300); }catch (InterruptedException e){ System.out.println("anotherThread interrupted"); return; } synchList.remove(1); synchList.remove(7); } }; anotherThread.start(); Iterator iterator = synchList.iterator(); while(iterator.hasNext()){ System.out.print(iterator.next()); try{ Thread.sleep(100); } catch (InterruptedException e) { System.out.println("Main thread interrupted"); return; } } } static void fillList(List list, int n){ for(int i = 1; i <= n; i++) list.add(i); } }
Concurrent collections
← Prev
1
Next →
Sign Up Now
or
Subscribe for future quizzes