Why can't we call Thread#sleep() directly inside a Lambda function?
Clash Royale CLAN TAG#URR8PPP
up vote
11
down vote
favorite
The below code is giving me compile time error:
The method sleep(int) is undefined for the type A (A is my class name).
Thread t2 = new Thread(() ->
try
sleep(1000);
catch (InterruptedException e)
);
Whereas, when I specify using Anonymous inner class, there is no compile time error:
Thread t1 = new Thread()
public void run()
try
sleep(1000);
catch (InterruptedException e)
;
The below code also works fine:
Thread t3 = new Thread(() -> System.out.println("In lambda"));
How things work inside a lamda expression body? Please, help.
From many answers, I can see that the error can be resolved using Thread.sleep(1000)
in my first approach. However, I'd really appreciate if someone could explain me how scope/context works in a lambda expression.
java multithreading lambda java-8
add a comment |Â
up vote
11
down vote
favorite
The below code is giving me compile time error:
The method sleep(int) is undefined for the type A (A is my class name).
Thread t2 = new Thread(() ->
try
sleep(1000);
catch (InterruptedException e)
);
Whereas, when I specify using Anonymous inner class, there is no compile time error:
Thread t1 = new Thread()
public void run()
try
sleep(1000);
catch (InterruptedException e)
;
The below code also works fine:
Thread t3 = new Thread(() -> System.out.println("In lambda"));
How things work inside a lamda expression body? Please, help.
From many answers, I can see that the error can be resolved using Thread.sleep(1000)
in my first approach. However, I'd really appreciate if someone could explain me how scope/context works in a lambda expression.
java multithreading lambda java-8
2
What is the compiler error you are getting?
– Ryan The Leach
1 hour ago
@RyanTheLeach, The method sleep(int) is undefined for the type A, Here A is my class Name.
– ChandraBhan Singh
58 mins ago
have you tried to import static java.lang.Thread.sleep;
– vmrvictor
55 mins ago
@Chandra, I've edited my answer in order to answer your new question. I hope it is of help. Cheers!
– Talendar
19 mins ago
add a comment |Â
up vote
11
down vote
favorite
up vote
11
down vote
favorite
The below code is giving me compile time error:
The method sleep(int) is undefined for the type A (A is my class name).
Thread t2 = new Thread(() ->
try
sleep(1000);
catch (InterruptedException e)
);
Whereas, when I specify using Anonymous inner class, there is no compile time error:
Thread t1 = new Thread()
public void run()
try
sleep(1000);
catch (InterruptedException e)
;
The below code also works fine:
Thread t3 = new Thread(() -> System.out.println("In lambda"));
How things work inside a lamda expression body? Please, help.
From many answers, I can see that the error can be resolved using Thread.sleep(1000)
in my first approach. However, I'd really appreciate if someone could explain me how scope/context works in a lambda expression.
java multithreading lambda java-8
The below code is giving me compile time error:
The method sleep(int) is undefined for the type A (A is my class name).
Thread t2 = new Thread(() ->
try
sleep(1000);
catch (InterruptedException e)
);
Whereas, when I specify using Anonymous inner class, there is no compile time error:
Thread t1 = new Thread()
public void run()
try
sleep(1000);
catch (InterruptedException e)
;
The below code also works fine:
Thread t3 = new Thread(() -> System.out.println("In lambda"));
How things work inside a lamda expression body? Please, help.
From many answers, I can see that the error can be resolved using Thread.sleep(1000)
in my first approach. However, I'd really appreciate if someone could explain me how scope/context works in a lambda expression.
java multithreading lambda java-8
java multithreading lambda java-8
edited 15 mins ago


Talendar
602215
602215
asked 1 hour ago
ChandraBhan Singh
9521118
9521118
2
What is the compiler error you are getting?
– Ryan The Leach
1 hour ago
@RyanTheLeach, The method sleep(int) is undefined for the type A, Here A is my class Name.
– ChandraBhan Singh
58 mins ago
have you tried to import static java.lang.Thread.sleep;
– vmrvictor
55 mins ago
@Chandra, I've edited my answer in order to answer your new question. I hope it is of help. Cheers!
– Talendar
19 mins ago
add a comment |Â
2
What is the compiler error you are getting?
– Ryan The Leach
1 hour ago
@RyanTheLeach, The method sleep(int) is undefined for the type A, Here A is my class Name.
– ChandraBhan Singh
58 mins ago
have you tried to import static java.lang.Thread.sleep;
– vmrvictor
55 mins ago
@Chandra, I've edited my answer in order to answer your new question. I hope it is of help. Cheers!
– Talendar
19 mins ago
2
2
What is the compiler error you are getting?
– Ryan The Leach
1 hour ago
What is the compiler error you are getting?
– Ryan The Leach
1 hour ago
@RyanTheLeach, The method sleep(int) is undefined for the type A, Here A is my class Name.
– ChandraBhan Singh
58 mins ago
@RyanTheLeach, The method sleep(int) is undefined for the type A, Here A is my class Name.
– ChandraBhan Singh
58 mins ago
have you tried to import static java.lang.Thread.sleep;
– vmrvictor
55 mins ago
have you tried to import static java.lang.Thread.sleep;
– vmrvictor
55 mins ago
@Chandra, I've edited my answer in order to answer your new question. I hope it is of help. Cheers!
– Talendar
19 mins ago
@Chandra, I've edited my answer in order to answer your new question. I hope it is of help. Cheers!
– Talendar
19 mins ago
add a comment |Â
6 Answers
6
active
oldest
votes
up vote
3
down vote
accepted
Your doubt is generated by a misunderstanding about how the scopes of a lambda expression and an anonymous class work. Below, I will try to clarify this.
Lambda expressions DO NOT introduce a new level of scoping. This means that, inside it, you can only access the same things that you would be able to access in the immediately enclosing code block. See what the docs say:
Lambda expressions are lexically scoped. This means that they do not
inherit any names from a supertype or introduce a new level of
scoping. Declarations in a lambda expression are interpreted just as
they are in the enclosing environment.
Anonymous classes work differently. They do introduce a new level of scoping. They behave much like a local class (a class that you declare inside a block of code), although they can't have constructors. See what the docs say:
Like local classes, anonymous classes can capture variables; they have the same access to local variables of the enclosing scope:
- An anonymous class has access to the members of its enclosing class.
- An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.
- Like a nested class, a declaration of a type (such as a variable) in an anonymous class shadows any other declarations in the enclosing
scope that have the same name. See Shadowing for more information.
In this context, I can now answer your question. The anonymous class will act like a local class inside Thread
and, thus, it will be able to access sleep()
directly, since it will be within it's scope. However, in the lambda expression, sleep()
isn't available within it's scope (you wouldn't be able to call sleep()
directly on the line before the lambda, for example), so that you must use Thread.sleep()
.
add a comment |Â
up vote
8
down vote
Thread.sleep
is a static method in the Thread
class.
The reason you can call sleep
directly without any qualifiers in an anonymous class is because you are actually in the context of a class that inherits from Thread
. Therefore, sleep
is accessible there.
But in the lambda case, you are not in a class that inherits from Thread
. You are inside whatever class is surrounding that code. Therefore, sleep
can't be called directly and you need to say Thread.sleep
. The documentation also supports this:
Lambda expressions are lexically scoped. This means that they do not
inherit any names from a supertype or introduce a new level of
scoping. Declarations in a lambda expression are interpreted just as
they are in the enclosing environment.
Basically that is saying that inside the lambda, you are actually in the same scope as if you were outside of the lambda. If you can't access sleep
outside the lambda, you can't on the inside either.
Also, note that the two ways of creating a thread that you have shown here is intrinsically different. In the lambda one, you are passing a Runnable
to the Thread
constructor, whereas in the anonymous class one, you are creating a Thread
by creating an anonymous class of it directly.
1
The documentation says: the lambda expression does not introduce a new level of scoping. Consequently, you can directly access fields, methods, and local variables of the enclosing scope.
– LuCio
46 mins ago
@LuCio Thanks! edited that into the answer.
– Sweeper
41 mins ago
You can still useimport static java.lang.Thread.sleep;
. But having to catchInterruptedException
creates far more visual noise than a precedingThread.
. So, to create a new thread which just sleeps one second, I’d usenew Thread(() -> LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1)))
instead.
– Holger
33 mins ago
add a comment |Â
up vote
2
down vote
In the first approach, you are passing a Runnable
to the Thread
, you need call Thread.sleep
:
Thread t2 = new Thread(() ->
try
Thread.sleep(1000);
catch (InterruptedException e)
);
it is the short version of:
Runnable runnable = new Runnable()
public void run()
try
Thread.sleep(1000);
catch (InterruptedException e)
;
Thread t2 = new Thread(runnable);
While in the second, you are overriding thread.run
method directly, so it's ok to call thread.sleep
in thread.run
.
add a comment |Â
up vote
2
down vote
This ends up being a misunderstanding of Scope.
When you are passing in a lambda to the thread, you are not creating a subclass of Thread, instead you are passing in the FunctionalInterface of Runnable and calling a constructor of Thread. When you attempt to call Sleep, the context of your scope is a combination of Runnable + your class (you can call default methods if the Runnable class had them), not Thread.
Runnable does not have sleep() defined, but Thread does.
When you create an anonymous inner class, you are subclassing Thread, so sleep() is available for you to call, as the context of the Scope is a subclass of Thread.
Calling static methods, without the class name is discouraged for exactly this kind of misunderstanding. Using Thread.Sleep is both correct, and unambiguous in all circumstances.
add a comment |Â
up vote
1
down vote
Following code works:
Thread t2 = new Thread(() ->
try
Thread.sleep(1000);
catch (InterruptedException e)
);
This is because sleep(int milliseconds)
is a method from Thread
class while you are creating and passing a Runnable
instance to Thread
class constructor.
In the second method, You are creating an anonymous inner class instance of Thread
class and thus have access to all Thread
class methods.
add a comment |Â
up vote
0
down vote
Thread.sleep is static method ...
Thread t2 = new Thread(() ->
try
Thread.sleep(1000);
catch (InterruptedException e)
);
that is not the point. It is a method of Thread but the Lambda is being defined in the user class, just being used to create a Thread
– Carlos Heuberger
15 mins ago
add a comment |Â
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Your doubt is generated by a misunderstanding about how the scopes of a lambda expression and an anonymous class work. Below, I will try to clarify this.
Lambda expressions DO NOT introduce a new level of scoping. This means that, inside it, you can only access the same things that you would be able to access in the immediately enclosing code block. See what the docs say:
Lambda expressions are lexically scoped. This means that they do not
inherit any names from a supertype or introduce a new level of
scoping. Declarations in a lambda expression are interpreted just as
they are in the enclosing environment.
Anonymous classes work differently. They do introduce a new level of scoping. They behave much like a local class (a class that you declare inside a block of code), although they can't have constructors. See what the docs say:
Like local classes, anonymous classes can capture variables; they have the same access to local variables of the enclosing scope:
- An anonymous class has access to the members of its enclosing class.
- An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.
- Like a nested class, a declaration of a type (such as a variable) in an anonymous class shadows any other declarations in the enclosing
scope that have the same name. See Shadowing for more information.
In this context, I can now answer your question. The anonymous class will act like a local class inside Thread
and, thus, it will be able to access sleep()
directly, since it will be within it's scope. However, in the lambda expression, sleep()
isn't available within it's scope (you wouldn't be able to call sleep()
directly on the line before the lambda, for example), so that you must use Thread.sleep()
.
add a comment |Â
up vote
3
down vote
accepted
Your doubt is generated by a misunderstanding about how the scopes of a lambda expression and an anonymous class work. Below, I will try to clarify this.
Lambda expressions DO NOT introduce a new level of scoping. This means that, inside it, you can only access the same things that you would be able to access in the immediately enclosing code block. See what the docs say:
Lambda expressions are lexically scoped. This means that they do not
inherit any names from a supertype or introduce a new level of
scoping. Declarations in a lambda expression are interpreted just as
they are in the enclosing environment.
Anonymous classes work differently. They do introduce a new level of scoping. They behave much like a local class (a class that you declare inside a block of code), although they can't have constructors. See what the docs say:
Like local classes, anonymous classes can capture variables; they have the same access to local variables of the enclosing scope:
- An anonymous class has access to the members of its enclosing class.
- An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.
- Like a nested class, a declaration of a type (such as a variable) in an anonymous class shadows any other declarations in the enclosing
scope that have the same name. See Shadowing for more information.
In this context, I can now answer your question. The anonymous class will act like a local class inside Thread
and, thus, it will be able to access sleep()
directly, since it will be within it's scope. However, in the lambda expression, sleep()
isn't available within it's scope (you wouldn't be able to call sleep()
directly on the line before the lambda, for example), so that you must use Thread.sleep()
.
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Your doubt is generated by a misunderstanding about how the scopes of a lambda expression and an anonymous class work. Below, I will try to clarify this.
Lambda expressions DO NOT introduce a new level of scoping. This means that, inside it, you can only access the same things that you would be able to access in the immediately enclosing code block. See what the docs say:
Lambda expressions are lexically scoped. This means that they do not
inherit any names from a supertype or introduce a new level of
scoping. Declarations in a lambda expression are interpreted just as
they are in the enclosing environment.
Anonymous classes work differently. They do introduce a new level of scoping. They behave much like a local class (a class that you declare inside a block of code), although they can't have constructors. See what the docs say:
Like local classes, anonymous classes can capture variables; they have the same access to local variables of the enclosing scope:
- An anonymous class has access to the members of its enclosing class.
- An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.
- Like a nested class, a declaration of a type (such as a variable) in an anonymous class shadows any other declarations in the enclosing
scope that have the same name. See Shadowing for more information.
In this context, I can now answer your question. The anonymous class will act like a local class inside Thread
and, thus, it will be able to access sleep()
directly, since it will be within it's scope. However, in the lambda expression, sleep()
isn't available within it's scope (you wouldn't be able to call sleep()
directly on the line before the lambda, for example), so that you must use Thread.sleep()
.
Your doubt is generated by a misunderstanding about how the scopes of a lambda expression and an anonymous class work. Below, I will try to clarify this.
Lambda expressions DO NOT introduce a new level of scoping. This means that, inside it, you can only access the same things that you would be able to access in the immediately enclosing code block. See what the docs say:
Lambda expressions are lexically scoped. This means that they do not
inherit any names from a supertype or introduce a new level of
scoping. Declarations in a lambda expression are interpreted just as
they are in the enclosing environment.
Anonymous classes work differently. They do introduce a new level of scoping. They behave much like a local class (a class that you declare inside a block of code), although they can't have constructors. See what the docs say:
Like local classes, anonymous classes can capture variables; they have the same access to local variables of the enclosing scope:
- An anonymous class has access to the members of its enclosing class.
- An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.
- Like a nested class, a declaration of a type (such as a variable) in an anonymous class shadows any other declarations in the enclosing
scope that have the same name. See Shadowing for more information.
In this context, I can now answer your question. The anonymous class will act like a local class inside Thread
and, thus, it will be able to access sleep()
directly, since it will be within it's scope. However, in the lambda expression, sleep()
isn't available within it's scope (you wouldn't be able to call sleep()
directly on the line before the lambda, for example), so that you must use Thread.sleep()
.
edited 13 mins ago
answered 55 mins ago


Talendar
602215
602215
add a comment |Â
add a comment |Â
up vote
8
down vote
Thread.sleep
is a static method in the Thread
class.
The reason you can call sleep
directly without any qualifiers in an anonymous class is because you are actually in the context of a class that inherits from Thread
. Therefore, sleep
is accessible there.
But in the lambda case, you are not in a class that inherits from Thread
. You are inside whatever class is surrounding that code. Therefore, sleep
can't be called directly and you need to say Thread.sleep
. The documentation also supports this:
Lambda expressions are lexically scoped. This means that they do not
inherit any names from a supertype or introduce a new level of
scoping. Declarations in a lambda expression are interpreted just as
they are in the enclosing environment.
Basically that is saying that inside the lambda, you are actually in the same scope as if you were outside of the lambda. If you can't access sleep
outside the lambda, you can't on the inside either.
Also, note that the two ways of creating a thread that you have shown here is intrinsically different. In the lambda one, you are passing a Runnable
to the Thread
constructor, whereas in the anonymous class one, you are creating a Thread
by creating an anonymous class of it directly.
1
The documentation says: the lambda expression does not introduce a new level of scoping. Consequently, you can directly access fields, methods, and local variables of the enclosing scope.
– LuCio
46 mins ago
@LuCio Thanks! edited that into the answer.
– Sweeper
41 mins ago
You can still useimport static java.lang.Thread.sleep;
. But having to catchInterruptedException
creates far more visual noise than a precedingThread.
. So, to create a new thread which just sleeps one second, I’d usenew Thread(() -> LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1)))
instead.
– Holger
33 mins ago
add a comment |Â
up vote
8
down vote
Thread.sleep
is a static method in the Thread
class.
The reason you can call sleep
directly without any qualifiers in an anonymous class is because you are actually in the context of a class that inherits from Thread
. Therefore, sleep
is accessible there.
But in the lambda case, you are not in a class that inherits from Thread
. You are inside whatever class is surrounding that code. Therefore, sleep
can't be called directly and you need to say Thread.sleep
. The documentation also supports this:
Lambda expressions are lexically scoped. This means that they do not
inherit any names from a supertype or introduce a new level of
scoping. Declarations in a lambda expression are interpreted just as
they are in the enclosing environment.
Basically that is saying that inside the lambda, you are actually in the same scope as if you were outside of the lambda. If you can't access sleep
outside the lambda, you can't on the inside either.
Also, note that the two ways of creating a thread that you have shown here is intrinsically different. In the lambda one, you are passing a Runnable
to the Thread
constructor, whereas in the anonymous class one, you are creating a Thread
by creating an anonymous class of it directly.
1
The documentation says: the lambda expression does not introduce a new level of scoping. Consequently, you can directly access fields, methods, and local variables of the enclosing scope.
– LuCio
46 mins ago
@LuCio Thanks! edited that into the answer.
– Sweeper
41 mins ago
You can still useimport static java.lang.Thread.sleep;
. But having to catchInterruptedException
creates far more visual noise than a precedingThread.
. So, to create a new thread which just sleeps one second, I’d usenew Thread(() -> LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1)))
instead.
– Holger
33 mins ago
add a comment |Â
up vote
8
down vote
up vote
8
down vote
Thread.sleep
is a static method in the Thread
class.
The reason you can call sleep
directly without any qualifiers in an anonymous class is because you are actually in the context of a class that inherits from Thread
. Therefore, sleep
is accessible there.
But in the lambda case, you are not in a class that inherits from Thread
. You are inside whatever class is surrounding that code. Therefore, sleep
can't be called directly and you need to say Thread.sleep
. The documentation also supports this:
Lambda expressions are lexically scoped. This means that they do not
inherit any names from a supertype or introduce a new level of
scoping. Declarations in a lambda expression are interpreted just as
they are in the enclosing environment.
Basically that is saying that inside the lambda, you are actually in the same scope as if you were outside of the lambda. If you can't access sleep
outside the lambda, you can't on the inside either.
Also, note that the two ways of creating a thread that you have shown here is intrinsically different. In the lambda one, you are passing a Runnable
to the Thread
constructor, whereas in the anonymous class one, you are creating a Thread
by creating an anonymous class of it directly.
Thread.sleep
is a static method in the Thread
class.
The reason you can call sleep
directly without any qualifiers in an anonymous class is because you are actually in the context of a class that inherits from Thread
. Therefore, sleep
is accessible there.
But in the lambda case, you are not in a class that inherits from Thread
. You are inside whatever class is surrounding that code. Therefore, sleep
can't be called directly and you need to say Thread.sleep
. The documentation also supports this:
Lambda expressions are lexically scoped. This means that they do not
inherit any names from a supertype or introduce a new level of
scoping. Declarations in a lambda expression are interpreted just as
they are in the enclosing environment.
Basically that is saying that inside the lambda, you are actually in the same scope as if you were outside of the lambda. If you can't access sleep
outside the lambda, you can't on the inside either.
Also, note that the two ways of creating a thread that you have shown here is intrinsically different. In the lambda one, you are passing a Runnable
to the Thread
constructor, whereas in the anonymous class one, you are creating a Thread
by creating an anonymous class of it directly.
edited 41 mins ago
answered 53 mins ago


Sweeper
56.7k962124
56.7k962124
1
The documentation says: the lambda expression does not introduce a new level of scoping. Consequently, you can directly access fields, methods, and local variables of the enclosing scope.
– LuCio
46 mins ago
@LuCio Thanks! edited that into the answer.
– Sweeper
41 mins ago
You can still useimport static java.lang.Thread.sleep;
. But having to catchInterruptedException
creates far more visual noise than a precedingThread.
. So, to create a new thread which just sleeps one second, I’d usenew Thread(() -> LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1)))
instead.
– Holger
33 mins ago
add a comment |Â
1
The documentation says: the lambda expression does not introduce a new level of scoping. Consequently, you can directly access fields, methods, and local variables of the enclosing scope.
– LuCio
46 mins ago
@LuCio Thanks! edited that into the answer.
– Sweeper
41 mins ago
You can still useimport static java.lang.Thread.sleep;
. But having to catchInterruptedException
creates far more visual noise than a precedingThread.
. So, to create a new thread which just sleeps one second, I’d usenew Thread(() -> LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1)))
instead.
– Holger
33 mins ago
1
1
The documentation says: the lambda expression does not introduce a new level of scoping. Consequently, you can directly access fields, methods, and local variables of the enclosing scope.
– LuCio
46 mins ago
The documentation says: the lambda expression does not introduce a new level of scoping. Consequently, you can directly access fields, methods, and local variables of the enclosing scope.
– LuCio
46 mins ago
@LuCio Thanks! edited that into the answer.
– Sweeper
41 mins ago
@LuCio Thanks! edited that into the answer.
– Sweeper
41 mins ago
You can still use
import static java.lang.Thread.sleep;
. But having to catch InterruptedException
creates far more visual noise than a preceding Thread.
. So, to create a new thread which just sleeps one second, I’d use new Thread(() -> LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1)))
instead.– Holger
33 mins ago
You can still use
import static java.lang.Thread.sleep;
. But having to catch InterruptedException
creates far more visual noise than a preceding Thread.
. So, to create a new thread which just sleeps one second, I’d use new Thread(() -> LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1)))
instead.– Holger
33 mins ago
add a comment |Â
up vote
2
down vote
In the first approach, you are passing a Runnable
to the Thread
, you need call Thread.sleep
:
Thread t2 = new Thread(() ->
try
Thread.sleep(1000);
catch (InterruptedException e)
);
it is the short version of:
Runnable runnable = new Runnable()
public void run()
try
Thread.sleep(1000);
catch (InterruptedException e)
;
Thread t2 = new Thread(runnable);
While in the second, you are overriding thread.run
method directly, so it's ok to call thread.sleep
in thread.run
.
add a comment |Â
up vote
2
down vote
In the first approach, you are passing a Runnable
to the Thread
, you need call Thread.sleep
:
Thread t2 = new Thread(() ->
try
Thread.sleep(1000);
catch (InterruptedException e)
);
it is the short version of:
Runnable runnable = new Runnable()
public void run()
try
Thread.sleep(1000);
catch (InterruptedException e)
;
Thread t2 = new Thread(runnable);
While in the second, you are overriding thread.run
method directly, so it's ok to call thread.sleep
in thread.run
.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
In the first approach, you are passing a Runnable
to the Thread
, you need call Thread.sleep
:
Thread t2 = new Thread(() ->
try
Thread.sleep(1000);
catch (InterruptedException e)
);
it is the short version of:
Runnable runnable = new Runnable()
public void run()
try
Thread.sleep(1000);
catch (InterruptedException e)
;
Thread t2 = new Thread(runnable);
While in the second, you are overriding thread.run
method directly, so it's ok to call thread.sleep
in thread.run
.
In the first approach, you are passing a Runnable
to the Thread
, you need call Thread.sleep
:
Thread t2 = new Thread(() ->
try
Thread.sleep(1000);
catch (InterruptedException e)
);
it is the short version of:
Runnable runnable = new Runnable()
public void run()
try
Thread.sleep(1000);
catch (InterruptedException e)
;
Thread t2 = new Thread(runnable);
While in the second, you are overriding thread.run
method directly, so it's ok to call thread.sleep
in thread.run
.
answered 58 mins ago
Sun
14.2k31541
14.2k31541
add a comment |Â
add a comment |Â
up vote
2
down vote
This ends up being a misunderstanding of Scope.
When you are passing in a lambda to the thread, you are not creating a subclass of Thread, instead you are passing in the FunctionalInterface of Runnable and calling a constructor of Thread. When you attempt to call Sleep, the context of your scope is a combination of Runnable + your class (you can call default methods if the Runnable class had them), not Thread.
Runnable does not have sleep() defined, but Thread does.
When you create an anonymous inner class, you are subclassing Thread, so sleep() is available for you to call, as the context of the Scope is a subclass of Thread.
Calling static methods, without the class name is discouraged for exactly this kind of misunderstanding. Using Thread.Sleep is both correct, and unambiguous in all circumstances.
add a comment |Â
up vote
2
down vote
This ends up being a misunderstanding of Scope.
When you are passing in a lambda to the thread, you are not creating a subclass of Thread, instead you are passing in the FunctionalInterface of Runnable and calling a constructor of Thread. When you attempt to call Sleep, the context of your scope is a combination of Runnable + your class (you can call default methods if the Runnable class had them), not Thread.
Runnable does not have sleep() defined, but Thread does.
When you create an anonymous inner class, you are subclassing Thread, so sleep() is available for you to call, as the context of the Scope is a subclass of Thread.
Calling static methods, without the class name is discouraged for exactly this kind of misunderstanding. Using Thread.Sleep is both correct, and unambiguous in all circumstances.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
This ends up being a misunderstanding of Scope.
When you are passing in a lambda to the thread, you are not creating a subclass of Thread, instead you are passing in the FunctionalInterface of Runnable and calling a constructor of Thread. When you attempt to call Sleep, the context of your scope is a combination of Runnable + your class (you can call default methods if the Runnable class had them), not Thread.
Runnable does not have sleep() defined, but Thread does.
When you create an anonymous inner class, you are subclassing Thread, so sleep() is available for you to call, as the context of the Scope is a subclass of Thread.
Calling static methods, without the class name is discouraged for exactly this kind of misunderstanding. Using Thread.Sleep is both correct, and unambiguous in all circumstances.
This ends up being a misunderstanding of Scope.
When you are passing in a lambda to the thread, you are not creating a subclass of Thread, instead you are passing in the FunctionalInterface of Runnable and calling a constructor of Thread. When you attempt to call Sleep, the context of your scope is a combination of Runnable + your class (you can call default methods if the Runnable class had them), not Thread.
Runnable does not have sleep() defined, but Thread does.
When you create an anonymous inner class, you are subclassing Thread, so sleep() is available for you to call, as the context of the Scope is a subclass of Thread.
Calling static methods, without the class name is discouraged for exactly this kind of misunderstanding. Using Thread.Sleep is both correct, and unambiguous in all circumstances.
edited 15 mins ago
answered 54 mins ago
Ryan The Leach
2,33411736
2,33411736
add a comment |Â
add a comment |Â
up vote
1
down vote
Following code works:
Thread t2 = new Thread(() ->
try
Thread.sleep(1000);
catch (InterruptedException e)
);
This is because sleep(int milliseconds)
is a method from Thread
class while you are creating and passing a Runnable
instance to Thread
class constructor.
In the second method, You are creating an anonymous inner class instance of Thread
class and thus have access to all Thread
class methods.
add a comment |Â
up vote
1
down vote
Following code works:
Thread t2 = new Thread(() ->
try
Thread.sleep(1000);
catch (InterruptedException e)
);
This is because sleep(int milliseconds)
is a method from Thread
class while you are creating and passing a Runnable
instance to Thread
class constructor.
In the second method, You are creating an anonymous inner class instance of Thread
class and thus have access to all Thread
class methods.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Following code works:
Thread t2 = new Thread(() ->
try
Thread.sleep(1000);
catch (InterruptedException e)
);
This is because sleep(int milliseconds)
is a method from Thread
class while you are creating and passing a Runnable
instance to Thread
class constructor.
In the second method, You are creating an anonymous inner class instance of Thread
class and thus have access to all Thread
class methods.
Following code works:
Thread t2 = new Thread(() ->
try
Thread.sleep(1000);
catch (InterruptedException e)
);
This is because sleep(int milliseconds)
is a method from Thread
class while you are creating and passing a Runnable
instance to Thread
class constructor.
In the second method, You are creating an anonymous inner class instance of Thread
class and thus have access to all Thread
class methods.
answered 54 mins ago
S.K.
1,853717
1,853717
add a comment |Â
add a comment |Â
up vote
0
down vote
Thread.sleep is static method ...
Thread t2 = new Thread(() ->
try
Thread.sleep(1000);
catch (InterruptedException e)
);
that is not the point. It is a method of Thread but the Lambda is being defined in the user class, just being used to create a Thread
– Carlos Heuberger
15 mins ago
add a comment |Â
up vote
0
down vote
Thread.sleep is static method ...
Thread t2 = new Thread(() ->
try
Thread.sleep(1000);
catch (InterruptedException e)
);
that is not the point. It is a method of Thread but the Lambda is being defined in the user class, just being used to create a Thread
– Carlos Heuberger
15 mins ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Thread.sleep is static method ...
Thread t2 = new Thread(() ->
try
Thread.sleep(1000);
catch (InterruptedException e)
);
Thread.sleep is static method ...
Thread t2 = new Thread(() ->
try
Thread.sleep(1000);
catch (InterruptedException e)
);
answered 59 mins ago


niemar
152213
152213
that is not the point. It is a method of Thread but the Lambda is being defined in the user class, just being used to create a Thread
– Carlos Heuberger
15 mins ago
add a comment |Â
that is not the point. It is a method of Thread but the Lambda is being defined in the user class, just being used to create a Thread
– Carlos Heuberger
15 mins ago
that is not the point. It is a method of Thread but the Lambda is being defined in the user class, just being used to create a Thread
– Carlos Heuberger
15 mins ago
that is not the point. It is a method of Thread but the Lambda is being defined in the user class, just being used to create a Thread
– Carlos Heuberger
15 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52602990%2fwhy-cant-we-call-threadsleep-directly-inside-a-lambda-function%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
What is the compiler error you are getting?
– Ryan The Leach
1 hour ago
@RyanTheLeach, The method sleep(int) is undefined for the type A, Here A is my class Name.
– ChandraBhan Singh
58 mins ago
have you tried to import static java.lang.Thread.sleep;
– vmrvictor
55 mins ago
@Chandra, I've edited my answer in order to answer your new question. I hope it is of help. Cheers!
– Talendar
19 mins ago