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..

Answer

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..

Leave a Reply

Your email address will not be published. Required fields are marked *