Home arrow static arrow Java Programming [Archive] - New improvement for Java syntax
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - New improvement for Java syntax
This topic has 3 replies on 1 page.

Posts:1
Registered: 6/30/04
New improvement for Java syntax  
Jun 30, 2004 2:19 AM



 
Hi developers

I develop in java each day and the new project that I am envolved uses too much multithreading. So i thought that a litle improvement could be made on java syntax.

When two threads uses the same class attribute we must synchronize the access to this.

class myClass {

...
private int myAtribute = 0;
...

thread1: We must access the attribute synchronized....

...
shynchronized (this) {
myAttribute = 1;
}
...

thread2: The same....

...
synchronized (this) {
myAttribute = 3;
}
...

thread2: But if i don't care.... :-(

...
myAttribute = 4;
...

}

Ok, that is the problem.

I thought to solve this the java language must give us the chance to declare attributes in the way...

private synchronize int myAttribute = 0;

to be only accesed by sinchronized methods or by synchronized blocks in order to force interpreter to check if any method access this attribute without being synchronized in compiling time.

 

Posts:4,689
Registered: 6/6/03
Re: New improvement for Java syntax  
Jun 30, 2004 2:29 AM (reply 1 of 3)



 
But it matters a lot on which object the block is synchronized. And it remains an issue even with your new keyword. I do not like new keyword proposals anyway.
 

Posts:2,909
Registered: 13.8.2003
Re: New improvement for Java syntax  
Jun 30, 2004 2:33 AM (reply 2 of 3)



 
besides, most often the synchronized keyword is not used like the original poster showed.

REQUEST FOR NEW KEYWORD DENIED
 

Posts:342
Registered: 5/22/98
Re: New improvement for Java syntax  
Jun 30, 2004 3:08 AM (reply 3 of 3)



 
In your example, wouldn't the volatile keyword normally suffice?
private volatile int myAttribute = 0;


Graeme
 
This topic has 3 replies on 1 page.