From e036a1adeb6d775d3e6e1f01651d42ab5f5fb1d7 Mon Sep 17 00:00:00 2001 From: Mona Mohamadinia Date: Sat, 20 Jun 2020 21:05:51 +0430 Subject: [PATCH] Added Code Samples (#9498) --- .../baeldung/thisescape/ImplicitEscape.java | 16 +++++++++++++ .../baeldung/thisescape/LoggerRunnable.java | 14 +++++++++++ .../baeldung/thisescape/SafePublication.java | 24 +++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/thisescape/ImplicitEscape.java create mode 100644 core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/thisescape/LoggerRunnable.java create mode 100644 core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/thisescape/SafePublication.java diff --git a/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/thisescape/ImplicitEscape.java b/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/thisescape/ImplicitEscape.java new file mode 100644 index 0000000000..0fc71c86d5 --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/thisescape/ImplicitEscape.java @@ -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(); + } +} diff --git a/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/thisescape/LoggerRunnable.java b/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/thisescape/LoggerRunnable.java new file mode 100644 index 0000000000..d7805e09d7 --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/thisescape/LoggerRunnable.java @@ -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..."); + } +} diff --git a/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/thisescape/SafePublication.java b/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/thisescape/SafePublication.java new file mode 100644 index 0000000000..e54085afbb --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/thisescape/SafePublication.java @@ -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(); + } +}