@Override is a so-called annotation.
It does not generate any byte-code/runnable-code.
It just tells the compiler that the method below it is an override of
its super-class' method.
If your super-class changes its signature of onDraw, your compiler
will warn you about the fact that your method (in this case onDraw) no
longer overloads the super-class version of onDraw.

Try it your self: change 'protected void onDraw(Canvas canvas)' into
'protected boolean onDraw(Canvas canvas)' and do a 'return true;' at
the bottom of this method. You'll get a compiler error.

You can leave @Override out. It's just a safeguard.

synchronized:
I'd suggest your starting reading about Java Synchronization.
'synchronized' blocks prevent multiple threads from executing the same
block of code at the same time. They are used to synchronize access to
methods and instance-variables and avoid race-conditions. Careless use
of 'synchronized' block, however, could cause dead-locks.


E.g.
public synchronized boolean isPaused() {
  return mPaused;
}

public synchronized void pause() {
  mPaused = true;
}

This makes sure that mPaused is always read and/or updated by one
thread at a time. If thread-1 is inside isPaused() and thread-2 is
trying to call isPaused() or pause(), thread-2 waits until thread-1
exits isPaused().

A thread entering a synchronized block, obtains a lock on the object
specified (in above example, the object is 'this'). Any other thread
trying to do the same has to wait.
A thread exiting a synchronized block, releases the lock on the
object. Any waiting thread will now wake up and try to obtain the
lock.

BTW:
public synchronized void someMethod() {
  ...
}

is the same as
public synchronized void someMethod() {
  synchronized(this) {
    ...
  }
}

and
public static synchronized void someStaticMethod() {
  ...
}

is the same as
public static synchronized void someStaticMethod() {
  synchronized(MyClass.class) {
    ...
  }
}

On Apr 1, 3:29 am, Bin Chen <binary.c...@gmail.com> wrote:
> I am a C programmer before, and I am looking into android source code
> right now, some JAVA syntax is confusing, I am not sure whether or not
> it's android related, see:
>
>         @Override
>         protected void onDraw(Canvas canvas) {
>             synchronized (this) {
>                 if (mBitmap != null) {
>                     final Paint paint = mPaint;
>                     final Path path = mPath;
>                     final int outer = 0xFFC0C0C0;
>                     final int inner = 0xFFff7010;
>
> 1) What's the meaning of Override? Is it ommitable?
> 2) What does the "synchronized" mean?
>
> Thanks.
> Bin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to