Showing posts with label start. Show all posts
Showing posts with label start. Show all posts

Saturday, 9 May 2020

docker: create, start: Create and start the container

'docker create' command is used to create a container, once the container is created, you can start the container using 'docker start' command.

Step 1: Execute the below command to create a container
docker create -p1234:8080 jboss/wildfly

$docker create -p1234:8080 jboss/wildfly
a523924b741accfb04a72d6322c6197c50b4be0b175b8a2d9e087d22d20a1786
$
$docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES
a523924b741a        jboss/wildfly       "/opt/jboss/wildfly/…"   3 seconds ago       Created                                              silly_cori

From the output, you can observe that container is created with id 'a523924b741a'.

Step 2: Start the container using 'docker start' command.
$docker start a523924b741a
a523924b741a

open the url 'http://localhost:1234/', you can able to see WildFly server home page.


You can even execute the command 'docker ps -a' to check whether container started or not.
$docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                           PORTS                    NAMES
a523924b741a        jboss/wildfly       "/opt/jboss/wildfly/…"   About a minute ago   Up 50 seconds                    0.0.0.0:1234->8080/tcp   silly_cori



Previous                                                    Next                                                    Home

Sunday, 29 March 2020

Thread.start() vs Runnable.run()

Difference 1: Thread.start() execute the run() method in separate thread.

Thread start() method create new thread, which will call the run method internally. But when you execute 'run()' method of Runnable object directly, then it execute by the same thread that invokes run() method.

Example
Runnable runnable1 = .....;
Runnable runnable2 = .....;

When you call run method of Runnables runnable1, runnable2 directly from the main method, then these methods get executed by main thread.

App.java
package com.sample.app;

public class App {

	public static void main(String args[]) {
		Runnable runnable1 = () -> {
			Thread currentThread = Thread.currentThread();

			System.out.println("Runnable 1 is executed by : " + currentThread.getName());
		};

		Runnable runnable2 = () -> {
			Thread currentThread = Thread.currentThread();

			System.out.println("Runnable 2 is executed by : " + currentThread.getName());
		};

		runnable1.run();
		runnable2.run();

	}

}

Output
Runnable 1 is executed by : main
Runnable 2 is executed by : main

Passing Runnables to a thread
Thread thread1 = new Thread(runnable1);
Thread thread2 = new Thread(runnable2);

When you call thread1.start(), it starts a new thread and calls the run() method of runnable runnable1 within the new thread.

App.java
package com.sample.app;

public class App {

	public static void main(String args[]) {
		Runnable runnable1 = () -> {
			Thread currentThread = Thread.currentThread();

			System.out.println("Runnable 1 is executed by : " + currentThread.getName());
		};

		Runnable runnable2 = () -> {
			Thread currentThread = Thread.currentThread();

			System.out.println("Runnable 2 is executed by : " + currentThread.getName());
		};

		Thread thread1 = new Thread(runnable1);
		Thread thread2 = new Thread(runnable2);

		thread1.start();
		thread2.start();

	}

}

Output
Runnable 1 is executed by : Thread-0
Runnable 2 is executed by : Thread-1

Difference 2: run() method can be called any number of times, but start() method can be called atmost once.

If you call start() method of already started thread, then IllegalThreadStateException is thrown.

App.java
package com.sample.app;

public class App {

	public static void main(String args[]) {
		Runnable runnable1 = () -> {
			Thread currentThread = Thread.currentThread();

			System.out.println("Runnable 1 is executed by : " + currentThread.getName());
		};

		runnable1.run();
		runnable1.run();

		Thread thread1 = new Thread(runnable1);

		thread1.start();
		thread1.start();

	}

}

Output
Runnable 1 is executed by : main
Runnable 1 is executed by : main
Runnable 1 is executed by : Thread-0
Exception in thread "main" java.lang.IllegalThreadStateException
	at java.lang.Thread.start(Thread.java:708)
	at com.sample.app.App.main(App.java:18)

Difference 3: Most of the code of Thread start() method written in native language.

When you see the definition of start() method of Thread class, it looks like below.
public synchronized void start() {
		if (threadStatus != 0)
			throw new IllegalThreadStateException();

		group.add(this);

		boolean started = false;
		try {
			start0();
			started = true;
		} finally {
			try {
				if (!started) {
					group.threadStartFailed(this);
				}
			} catch (Throwable ignore) {

			}
		}
}

private native void start0();

As you see above definition, actual logic of thread starting (start0()) method is written in native language. Whereas definition of run method will be written in pure Java.


You may like