Creating Threads in Java or C++: Balancing Writing and Deleting in Shared Files

In summary, the conversation is about seeking help with a programming assignment that involves creating and managing threads to read and write to a shared file. The person asking for help is worried about not being able to complete the assignment in time and is directed to a different forum for assistance.
  • #1
kings1703
4
0
Java or C++ - Threads HELP!

I need help with this program. I got five others to do and I won't have time to do it and it's apart of my final exam for next Tuesday, so here is my question I need:

Create 5 threads which share a file and
let "even" threads write to a file, and let "odd" delete from a file.
Cycle 5 threads for 10 times. Make sure final file gives correct output. (no race conditions)

Please help me out!
 
Technology news on Phys.org
  • #3


thanks for that
 

Related to Creating Threads in Java or C++: Balancing Writing and Deleting in Shared Files

1. What are threads in Java and C++?

Threads are a way for a program to divide itself into two or more simultaneously running tasks. In Java, threads are a part of the language and are created using the "Thread" class. In C++, threads are implemented using the "thread" class from the standard library.

2. What is the difference between Java and C++ threads?

The main difference between Java and C++ threads is that Java threads are managed by the virtual machine, while C++ threads are managed by the operating system. This means that Java threads have a more consistent behavior across different platforms, while C++ threads may have different performance depending on the operating system.

3. How do I create and start a thread in Java or C++?

In Java, you can create a thread by creating a new instance of the "Thread" class and passing in a "Runnable" object as a parameter. Then, you can start the thread by calling the "start()" method on the thread object. In C++, you can create a thread by passing a function to the "thread" class constructor, and then calling the "join()" method to start the thread.

4. Can I use threads to improve performance in my program?

Yes, threads can improve performance in certain situations. In general, threads can help to increase the speed of a program by utilizing multiple processors or cores in a computer. However, it is important to note that using threads can also introduce complexity and potential bugs, so it is important to carefully consider whether using threads is necessary for your program.

5. Are there any potential issues or challenges when using threads in Java or C++?

Yes, there are a few potential issues to be aware of when using threads. One issue is synchronization, which is the process of coordinating access to shared resources between threads. This can be challenging to implement correctly and can lead to bugs such as race conditions. Another challenge is dealing with thread overhead, which refers to the additional resources and time needed to manage threads. This can impact performance and should be considered when deciding whether to use threads in a program.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
33
Views
7K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
Back
Top