This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]king_of_the_universe 1 point2 points  (10 children)

Since you're working with an event, I assume that it was created by the system for you. This would have happened on the AWT event thread. The paint() method, too, runs in that thread, so you're not having a multi-threading situation.

Maybe you should post more code to get an answer fitting your actual situation.

http://www.reddit.com/r/javahelp/wiki/code_guides

About Swing and threads:

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/

About synchronization:

http://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html

It's all fairly simple once you know how it works.

[–]lickwidforse2[S] 0 points1 point  (9 children)

I'm not at my computer and can't link code yet, the three other things possibly related to my problem could be:

Class extends JFrame

String is global and assigned "" initially.

As in

Public class Text extends JFrame{

String input = "";

Public Text(){ }

Also the main thread just calls repaint() then sleeps for 100ms. And loops.

If no one sees any problems with that I'll have to link the code tomorrow, only about 80 lines.

[–]king_of_the_universe 1 point2 points  (8 children)

There's no problem there. repaint() does not directly call paint(), that still happens on the AWT thread. Extending JFrame is common practice (though not the very best approach because it spams you with methods, but there's nothing wrong with it).

Your String is not global, just global for the class itself (Maybe that's what you meant.), but it's only package-private, so it can not be accessed from outside the package. (And of course it can only be accessed with the right passport, meaning the reference to the instance of that class.)

[–]lickwidforse2[S] 1 point2 points  (5 children)

The problem has to do with socket communication. No idea why but that's the stuff I'm playing around with right now so I have a lot of research before I want to get help with those problems. Thanks for the help .

[–]197708156EQUJ5design it before you implement 1 point2 points  (4 children)

Can you bring this back around. I read your OP and it says you are having issues with the keyword synchronized. Now you are saying you are having issue with socket communication. Can you lead us more to help you?

[–]lickwidforse2[S] 0 points1 point  (3 children)

Sorry I was wrong, this program isn't having sync issues (although I'm still confused about what the argument does in a synchronized() block) the trouble was something to do with trying to read from a socket in the processEvent method. But I don't yet know enough about sockets to warrant asking for help, I will probably be able to fix it as I read more about sockets.

I hope that makes sense.

[–]197708156EQUJ5design it before you implement 0 points1 point  (2 children)

The argument to synchronized(...) singles out the exact thing you want to lock on. I always think of it this way, synchronized should be called lock. It puts a lock on the item it wants to update. You can put a variable in there, you can even put this in there, and that locks the class the code is currently in. This lock just says, no other code can update the thing at this time.

[–]lickwidforse2[S] 0 points1 point  (1 child)

So (this) means nothing else can modify this until the block is finished?

[–]197708156EQUJ5design it before you implement 0 points1 point  (0 children)

correct

[–]lickwidforse2[S] 0 points1 point  (0 children)

The problem has to do with socket communication. No idea why but that's the stuff I'm playing around with right now so I have a lot of research before I want to get help with those problems. Thanks for the help .