Hello, I'm stuck a little bit with the following code. What I want is that user2 is able to modify as well the file1, so that was the point of the exercise to show efficient concurrent access to the files. Any ideas how to solve this and what is the problem here ?
import java.util.Calendar; public class TestWiki { public static void main(String[] args) { // The repository final Wiki rep = new Wiki(); // user1 thread final User user1 = new User("user1", 1) { public void run() { try { TxtFile file1 = new TxtFile("file1", this, "file1 contents", Calendar.getInstance()); TxtFile file2 = new TxtFile("file2", this, "file2 contents", Calendar.getInstance()); // user1 adds two files file1, file2 to the repository rep.add(file1); rep.add(file2); System.out.println(file1.getFileName() + " : '" + rep.readCurrentVersion(file1) + "', added by: " + this.toString()); System.out.println(file2.getFileName() + " : '" + rep.readCurrentVersion(file2) + "', added by: " + this.toString()); // user1 modifies file1 rep.modify("file1", "contents of the file1 modified by user1"); System.out.println(file1.getFileName() + " (modified) : '" + rep.readCurrentVersion(file1) + "', added by: " + this.toString()); } catch (Exception e) { System.out.println(this.getUserName() + " " + e); } } }; // user2 thread final User user2 = new User("user2", 2) { public void run() { try { TxtFile file3 = new TxtFile("file3", this, "file3 contents", Calendar.getInstance()); // user2 adds file3 to the repository rep.add(file3); System.out.println(file3.getFileName() + " : '" + rep.readCurrentVersion(file3) + "', added by: " + this.toString()); //now, at the same time as user1, user2 wants to make changes in the file1 rep.modify("file1", "contents of the file1 modified by User2"); System.out.println(file1.getFileName() + " (modified) : '" + rep.readCurrentVersion(file1) + "', added by: " + this.toString()); } catch (Exception e) { System.out.println(this.getUserName() + " " + e); } } }; user1.start(); user2.start(); } }
error message:
Multiple markers at this line
- file1 cannot be resolved
- file1 cannot be resolved to a
variable
I know that the problem is somehow related to threads but I can't get it, where is the trick...
(attached img is not visible look at the imageshack.com)
http://imageshack.us/photo/my-images...1207at429.png/