Over a million developers have joined DZone.

Riddle me this: File access & Multi threading

DZone's Guide to

Riddle me this: File access & Multi threading

Free Resource
What would be the output of the following code?
var list = new List<Task>();

var fileStream = File.Open("test", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
for (int i = 0; i < 150; i++)
var index = i;
var task = Task.Factory.StartNew(() =>
var buffer = Encoding.ASCII.GetBytes(index + "\r\n");
fileStream.Write(buffer, 0, buffer.Length);


Please note that:

  • What order the data is saved is not important.
  • Preventing data corruption is important.

Can you guess?


Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}