Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: can't use the file declared in other thread

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default can't use the file declared in other thread

    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/
    Attached Images Attached Images
    Last edited by r0x; December 7th, 2011 at 10:37 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: can't use the file declared in other thread

    Your file variables are declared inside the try block of a run method. To make them available in a wider scope, declare them in a wider scope.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can't use the file declared in other thread

    ok, I can declare file variables even before user1 thread and it will be accessible by user2 BUT it would mean that user1 didn't create any files, and I want that user1 creates file1 & file2 inside of his thread or it doesn't work that way?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: can't use the file declared in other thread

    Huh? Creating an instance of File is not the same thing as creating a file on disk.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can't use the file declared in other thread

    import java.io.File;
    import java.util.Calendar;
     
    @SuppressWarnings("serial")
    public class TxtFile extends File implements Cloneable
    {
     
    	//Name of File
    	private String fileName;
    	//Code
    	private String content;
    	//Version of File
    	private int version = -1;
    	//User
    	private User user;
    	//Date
    	private Calendar date;
     
     
    	//First Constructor
    	//Creates TxtFile Object and creates file on Disk in Wiki Folder
    	public TxtFile(String fileName, User user, String content, Calendar date){
    		super(fileName +".txt");
     
    		this.date = date;
    		this.fileName = fileName;
    		this.content = content;
    		this.user = user;
     
    	}
     
     
    	//Second Constructor for the TxtFile Class
    	//Creates TxtFile Object, included version
    	public TxtFile(String fileName, int version, User user, String content, Calendar date){
    		super(fileName +".txt");
    		this.date = date;
    		this.fileName = fileName;
    		this.content = content;
    		this.version = version;
    		this.user = user;
    	}
     
    	//Getter & Setter
    	public String getFileName(){
    		return fileName;
    	}
     
    	public String getContent(){
    		return content;
    	}
     
    	public void setContent(String content){
    		this.content = content;
    	}
     
    	public int getVersion(){
    		return version;
    	}
     
    	public void setVersion(int version){
    		this.version = version;
    	}
     
    	public User getUser(){
    		return user;
    	}
     
    	public void setUser(User user){
    		this.user = user;
    	}
     
    	public Calendar getDate(){
    		return this.date;
    	}
     
     
    	//Clone method to prevent users to bypass the system by keeping reference to file inside repository <br>
    	public TxtFile clone() throws CloneNotSupportedException {
    	    return new TxtFile(fileName, version, user, content, date);
    	}
    }

    public class User extends Thread {
     
    	// Username
    	private String userName;
    	// user ID
    	private int ID;
     
     
    	public User(String userName, int ID){
    		this.userName = userName;
    		this.ID = ID;
    	}
     
    	//Getter & Setter
    	public String getUserName(){
    		return userName;
    	}
     
    	public int getID(){
    		return ID;
    	}
     
    	public String toString(){
    		return userName +" with ID:" +ID;
    	}
    }

    that's why there are two constructors in TxtFile class. So if I understand it correctly file variables should be declared outside of the user1 thread, only in this way they would be visible to user2 ?
    Last edited by r0x; December 7th, 2011 at 10:54 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: can't use the file declared in other thread

    Are you confusing the concepts of an execution time thread that allows method execution and an instance of a class that extends the Thread class?
    Defining and accessing variables defined in different classes is a standard compiler problem

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can't use the file declared in other thread

    ok thank you all for replies but is any solution to my question ?
    how user2 can access file1 and modify it concurrently with user1 ?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: can't use the file declared in other thread

    The code in the two threads should call a single synchronized method that does the updates to the disk file to prevent collisions. That assumes that there is some control over what order the data is written to the file or where in the file or that it doesn't matter if the data is intermixed.

  9. #9
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can't use the file declared in other thread

    ok, I have the method modify in Wiki class

    public synchronized void modify(String fileName, String newContent) throws Exception
    	{
    		if(!repository.containsKey(fileName))
    			throw new Exception("The file \"" +fileName +"\" doesnt exist");
     
    		SynchronizedStack<TxtFile> stack = repository.get(fileName);
    		TxtFile latestFile = stack.peek().clone();
    		latestFile.setContent(newContent);
     
    		this.add(latestFile);
    	}
    That method should normally modify files. Is it what you are talking about ? a single synchronized method that does the updates.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: can't use the file declared in other thread

    Having the update method be synchronized will prevent more than one thread executing it at the same time.

Similar Threads

  1. What is this thread for exactly?
    By javapenguin in forum Computer Support
    Replies: 2
    Last Post: July 2nd, 2011, 10:37 AM
  2. Thread Question
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 11th, 2011, 08:57 PM
  3. Thread to EDT
    By Asido in forum Threads
    Replies: 3
    Last Post: October 15th, 2010, 01:13 PM
  4. "previously declared string"?... help
    By Taylorleemusic in forum Object Oriented Programming
    Replies: 3
    Last Post: September 15th, 2010, 10:57 PM
  5. JFrame declared as setAlwaysOnTop doesn't stay on top during slide show
    By ravindra_appikatla in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2010, 09:08 AM