Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8343561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:06:05+00:00 2026-06-09T06:06:05+00:00

everyone. To Test a ArrayList returned by the Collections.synchronizedList() method, I defined a static

  • 0

everyone. To Test a ArrayList returned by the Collections.synchronizedList() method, I defined a static class as below.

static class ListWriter implements Runnable { 
    private List<Integer> list; 

    public ListWriter(List<Integer> list) { 
        this.list = list; 
    } 

    public void run() {  // I didn't use synchronized(list) {} block here.
        try { 
            for (int i = 0; i < 20; i++) { 
                list.add(i); 
                Thread.sleep(10); 
            } 
        } catch (Exception e) { 
            e.printStackTrace();
        }
    } 
} 

and a test method as below

private static void test1() throws Exception {
    List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
    Thread t1 = new Thread(new ListWriter(list)); 
    Thread t2 = new Thread(new ListWriter(list));

    t1.start(); 
    t2.start(); 
    t1.join(); 
    t2.join(); 
    for (int i = 0; i < list.size(); ++i) { 
        System.out.println(list.get(i)); 
    } 
}

The result is usually this:
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
19
19

But sometimes it will be like this:

0
0
1
1
2
3
2
3
4
5
4
5
6
7
6
8
7
9
8
10
9
11
10
12
11
13
12
14
13
15
14
15
16
17
16
17
18
19
18
19

Does these results indicates the process is thread-safe?
The point is that I didn’t use a synchronized block in the writing thread which is explicitly required for iterating over the synchronized ArrayList returned by Collection.synchronizedList() method. So do you think the process is thread-safe or do I need to use synchronized block anyway?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:06:08+00:00Added an answer on June 9, 2026 at 6:06 am

    The output is non-deterministic – it depends on the scheduling behaviour of the underlying operating system.
    The fact that sometime you get a certain sequence is just a coincidence.
    Your list is synchronised at creation by the Collections.synchronizedList so you don’t need any additional synchronisation.

    A rough test of the thread-safeness of your list would be to validate that the number of elements contained in the list equals 40 after both threads are done.

    As you noted, for concurrent reading you will need to synchronize on the returned list as stated in the documentation

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

everyone. Please see example below. I'd like to supply a string to 'schedule_action' method
Everyone knows public class Test<T> where T : SomeBaseClass But is there a way
Everyone at Class too big and hard to add new features is completely unphased
I believe everyone encounters a case when there's necessity to test internal wiring of
everyone, I have a big file in the format given below. The data is
Possible Duplicate: how to test php email using WAMP Hey everyone, First off I
Like everyone else, I need to test my code on Internet Explorer 6 and
Hello Everyone! I need to test if a remote file on a http server
everyone, I met a problem about APNs when I used the code below. I
everyone. This is a piece of code from using sqlite in android tutorial: return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.