← 返回首页
JavaPracticeNotes/Threads at master · yankeexe/JavaPracticeNotes · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

Latest commit

 

History

History
 master
Top

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
View all files

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(); } }

Footer

© 2026 GitHub, Inc.