GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — JavaPracticeNotes/Threads at master · yankeexe/JavaPracticeNotes

Expand file tree

/

Threads

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

Threads

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
RunThread.java
RunThread.java
 
 
ThreadDemo.java
ThreadDemo.java
 
 
readme.md
readme.md
 
 

readme.md

Threads

  • It is used for asynchronous behaviour.
  • Thread can be created using Runnable Interface or Extending Thread
  • Thread.sleep(mills) pauses the current thread execution for defined millseconds.
  • We can also use Thread.sleep(mills,nano) which indicates the time in millisecond and nanosecond.

ThreadDemo.java

class ThreadDemo extends Thread{
    String name; 
    ThreadDemo(String n){
        name = n; 
        System.out.println("Creating: " +name);
    }

    public void run(){
        System.out.println("Running: " + name);

        try{
            for(int i = 4; i> 0; i--){
                System.out.println("Thread: "+name+" Printing: " + i);
                Thread.sleep(500);
            }
        }
            catch(InterruptedException e)
            {
                System.out.println("Thread "+ name + " interupted!");
            }
            System.out.println("Thread "+name+" exiting");
    }
   
}

RunThread.java

class RunThread {
    public static void main(String[] args) {
        ThreadDemo x = new ThreadDemo("Thread 1");
        x.start();

        ThreadDemo y = new ThreadDemo("Thread 2");
        y.start();
    }
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.