Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.jenkov.java.concurrency.thread;

public class ThreadExample {
public static void main(String[] args) {
Thread thread = new Thread();
System.out.println(thread.getName());//Thread-0
thread.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.jenkov.java.concurrency.thread;

public class ThreadExample10 {
public static void main(String[] args) {
Runnable runnable = () ->{
for (int i = 0; i < 5; i++) {
sleep(1000);
System.out.println("Running");
}
};

Thread thread = new Thread( runnable );
thread.setDaemon(true);
thread.start();

// try{
// thread.join();
// } catch (InterruptedException e){
// e.printStackTrace();
// }
}


public static void sleep(long millis){
try{
Thread.sleep(millis);
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.jenkov.java.concurrency.thread;

public class ThreadExample2 {
public static class MyThread extends Thread{
public void run(){
System.out.println("MyThread running");
System.out.println("MyThread finished");
}
}

public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.jenkov.java.concurrency.thread;

public class ThreadExample3 {
public static class MyRunnable implements Runnable{
public void run(){
System.out.println("MyRunnable running");
System.out.println("MyRunnable finished");
}
}

public static void main(String[] args) {
Thread thread = new Thread( new MyRunnable() );
thread.start();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.jenkov.java.concurrency.thread;

public class ThreadExample4 {

public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("Runnable running");
System.out.println("Runnable finished");
}
};
Thread thread = new Thread( runnable );
thread.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.jenkov.java.concurrency.thread;

public class ThreadExample5 {
public static void main(String[] args) {
Runnable runnable = () ->{
System.out.println("Lambada running");
System.out.println("Lambada finished");
};
Thread thread = new Thread( runnable );
thread.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.jenkov.java.concurrency.thread;

public class ThreadExample6 {
public static void main(String[] args) {
Runnable runnable = () -> {
String threadName = Thread.currentThread().getName();
System.out.println(threadName + " running");
};
Thread thread = new Thread( runnable, "The Thread1");
thread.start();
Thread thread2 = new Thread( runnable, "The Thread2");
thread2.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.jenkov.java.concurrency.thread;

public class ThreadExample7 {
public static void main(String[] args) {
Runnable runnable = () -> {
String threadName = Thread.currentThread().getName();
System.out.println(threadName = " running");

try{
Thread.sleep(2000);
} catch (InterruptedException e){
e.printStackTrace();
}
System.out.println(threadName + " finished");
};
Thread thread = new Thread( runnable, "The Thread");
thread.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.jenkov.java.concurrency.thread;

public class ThreadExample8 {
public static class StoppableRunnable implements Runnable{
private boolean stopRequested = false;

public synchronized void requestStop(){
this.stopRequested = true;
}

public synchronized boolean isStopRequested(){
return this.stopRequested;
}

private void sleep(long millis){
try {
Thread.sleep(millis);
} catch( InterruptedException e) {
e.printStackTrace();
}
}

@Override
public void run() {
System.out.println("StoppableRunnable Running");
while ( !isStopRequested()){
sleep(1000);
System.out.println("...");
}
System.out.println("StoppableRunnable Stopped");
}
}

public static void main(String[] args) {
StoppableRunnable stoppableRunnable = new StoppableRunnable();
Thread thread = new Thread( stoppableRunnable);
thread.start();

try{
Thread.sleep(5000);//main thread
} catch (InterruptedException e){
e.printStackTrace();
}

System.out.println("requesting stop");
stoppableRunnable.requestStop();
System.out.println("stop requested");

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.jenkov.java.concurrency.thread;

public class ThreadExample9 {
public static void main(String[] args) {
Runnable runnable = () -> {
while(true){
sleep(1000);
System.out.println("Running");
}
};

Thread thread = new Thread( runnable );
thread.setDaemon(true);//set before start
thread.start();
sleep(3100);
System.out.println("3s passed");
}

public static void sleep(long millis){
try{
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}