Riddle me this: File access & Multi threading
Join the DZone community and get the full member experience.
Join For FreeWhat 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);
});
list.Add(task);
}
Task.WaitAll(list.ToArray());
fileStream.Flush(true);
fileStream.Dispose();
Please note that:
- What order the data is saved is not important.
- Preventing data corruption is important.
Can you guess?
Threading
Data corruption
Data (computing)
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Top 10 Pillars of Zero Trust Networks
-
Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
-
Mastering Time Series Analysis: Techniques, Models, and Strategies
-
Microservices With Apache Camel and Quarkus
Comments