Added Code Samples (#9498)
This commit is contained in:
parent
6072c25d97
commit
e036a1adeb
@ -0,0 +1,16 @@
|
||||
package com.baeldung.thisescape;
|
||||
|
||||
public class ImplicitEscape {
|
||||
|
||||
public ImplicitEscape() {
|
||||
Thread t = new Thread() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Started...");
|
||||
}
|
||||
};
|
||||
|
||||
t.start();
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.baeldung.thisescape;
|
||||
|
||||
public class LoggerRunnable implements Runnable {
|
||||
|
||||
public LoggerRunnable() {
|
||||
Thread thread = new Thread(this); // this escapes
|
||||
thread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Started...");
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.thisescape;
|
||||
|
||||
public class SafePublication implements Runnable {
|
||||
|
||||
private final Thread thread;
|
||||
|
||||
public SafePublication() {
|
||||
thread = new Thread(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Started...");
|
||||
}
|
||||
|
||||
public void start() {
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SafePublication publication = new SafePublication();
|
||||
publication.start();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user