Skip to main content

Setting the custom name for Thread in Java

Setting the custom name for Thread in Java. Printing the Thread running sequence.


package demo;
public class Main {
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                //execute after getting instruction from OS
                System.out.println("Inside and running thread " + Thread.currentThread().getName());
                System.out.println("Current priority " + Thread.currentThread().getPriority());
            }
        });
        thread1.setName("Custom Worker Thread");
        thread1.setPriority(Thread.MAX_PRIORITY);
        System.out.println("We are in thread " + Thread.currentThread().getName() + " before new thead");
        thread1.start(); //start the thread and inform the OS
        System.out.println("We are in thread " + Thread.currentThread().getName() + " after new thead");
        Thread.sleep(3000); //won't consume CPU resource
    }
}

Comments

Popular posts from this blog

Spring Security with Spring Boot and MySQL

Spring Security with Spring Boot and MySQL In this tutorial I'll show how to use Spring Security with Spring Boot and MySQL. We'll implement a UserDetailsService provided by Spring Security. It is easy to configure Spring Security with Spring Boot and MySQL. You can also check how to run Spring Boot application inside docker container Steps: 1. Create a Spring Boot project from start.spring.io with following dependencies in your pom.xml file. You can choose gradle also for project dependencies. <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- dev tools for hot reloading --> <dependency> <groupId>org.springframework.b...

How to run Spring Boot application inside docker container

Step by Step procedure in creating and running a simple Spring Boot application inside docker container This tutorial will show you on how to run simple Spring Boot application inside docker container. If you have not installed docker already, please install it first. I'm using Linux Mint 18.2. You can follow these steps on How To install Docker in Linux Mint 18.2 After you've successfully installed docker in your machine, let's create a docker image that will run inside the container. Create a simple Spring Boot project from here . Select Web as a dependency. Import the project into you IDE, I'm using Spring Tool Suite. Create a controller and name it HelloController. You can name your controller anything you like. Create a method that will map the request and return a response. Create Dockerfile at the root of the project. The name is important here, only the letter D is capital Make the following entries inside the Dockerfile #Enter all the requi...