简体   繁体   中英

Android/java beginner syntax question

so I was watching a tutorial and I am trying to understand this syntax

  Thread thread = new Thread()
    {     
  //if i declare variables here i get errors
            public void run()
            {
            // ....define variables in function
           }
    }

How does this code work? I think when the thread object is created, it defines adds or "appends" a function to its "list" of functions? So thread.run() or thread.start() can be called later. I'm not sure I'm guessing..can someone enlighten me. From the tutorial I watched in the run function he did this

//the same run that is in the Thread class
public void run()
{
  int timer = 0 ;
  while(timer < 5000)
  {
     sleep(100) ; 
     timer+=100;
  }
}

What does sleep do? I've googled it but couldn't find anything good. Does it basically stop the current application for 100 ms before it continues with the loop? which means sleep is called 50 times? I'm terrible at maths lol..but if that is so, is the code above equivalent to

public void run()
{
  int timer = 0 ;
  while(timer < 50)
  {
     sleep(100) ; 
     timer++;
  }
}

However I know the simplest method would just be to say sleep(5000) ;

Clearly, I'm a noob at this..so will appreciate any explanation..the thorough the better thanks. Sorry, no Idea how to use code tags..

its not possible because its like trying put instance variables inside an instance which is not

possible.. we are giving an implementation only for a single instance.. and you cannot have an

instance variable defined specifically for a single Object.. and sleep(100) stops the execution of

current thread for 100 milliseconds and it happens for 50 times.. that means 50*100 milliseconds..

ntc gave a great answer, but threads aren't always the best way for android. Look into AsyncTask.

What you see in the first sample of code that you posted is an Anonymous Class. Here is a small example and here is an explanation on techartifact .

Put simply, this lets you create some object that will extend some class (Thread, in your sample). You don't name this class which means you won't be able to use this very same class in other parts of the code.

Usually the parent class you choose has some methods that must be implemented. In your example, you had to implement run() . When you are done, you have an object that will behave as you defined without the need to create a dedicated class or a "named" inner class .

This has the same behaviour as creating another class that extends Thread and doing Thread thread = new MyThreadBehaviour(); .

The method run does what you thing it does and I like your code much better!

There might be a reason to sleep the thread 50 times for a period of 100ms. For example, if you are running many threads at the same time, stopping them several times for smaller periods will result in a different behaviour than stopping them one long period.

You're using the syntax of an anonymous class. Basically you're defining and creating an instance of a Thread subclass at the same time. The run method is overriding the run method in Thread, and so your new Thread object uses your run method. The Thread class is defined such that when Thread.start() is called, it will start the steps necessary to create a thread, and will start executing the run method.

Anonymous classes are very useful if you need to have a simple class that won't be reusable, such as your example or an event listener. These types of classes will likely be context specific, and will only be used the one place where you need it, so it is better to have the class concisely defined and instantiated in the location where it will be used rather than to explicitly define a named subclass and then create and instance of that subclass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM