java thread
package com.journaldev.threads;
public class ThreadSleep {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
Thread.sleep(2000);
System.out.println("Sleep time in ms = "+(System.currentTimeMillis()-start));
}
}
0
0
// Copy and test
// They run simultaneously
public static void main(String[] args) {
// How to create a thread
Thread thread = new Thread(new Runnable() {
@Override
// Loop running in thread
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("Printing plus " + i + " in a worker thread.");
try {
Thread.sleep(1000);
} catch(Exception e) {
e.printStackTrace();
}
}
}
});
thread.start();
// Loop running in main thread
for (int j = 0; j < 20 ; j++) {
System.out.println("Printing plus " + j + " in a main thread.");
try {
Thread.sleep(900);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Thank you!
0
0
3.88
8
public static void main(String[] args {
...
Thread t1= new Thread(...);
t1.start();
...
}
Thank you!
8
0
4.75
4
class ThreadJoining extends Thread
{
@Override
public void run()
{
for (int i = 0; i < 2; i++)
{
try
{
Thread.sleep(500);
System.out.println("Current Thread: "
+ Thread.currentThread().getName());
}
catch(Exception ex)
{
System.out.println("Exception has" +
" been caught" + ex);
}
System.out.println(i);
}
}
}
class GFG
{
public static void main (String[] args)
{
ThreadJoining t1 = new ThreadJoining();
ThreadJoining t2 = new ThreadJoining();
ThreadJoining t3 = new ThreadJoining();
t1.start();
try
{
System.out.println("Current Thread: "
+ Thread.currentThread().getName());
t1.join();
}
catch(Exception ex)
{
System.out.println("Exception has " +
"been caught" + ex);
}
t2.start();
try
{
System.out.println("Current Thread: "
+ Thread.currentThread().getName());
t2.join();
}
catch(Exception ex)
{
System.out.println("Exception has been" +
" caught" + ex);
}
t3.start();
}
}
Thank you!
4
0
4.56
9
public class ClassName extends Thread{
public void run(){
super.run();
//Your Code
}
}
public class Main{
public static void main(String[] args){
ClassName thread = new ClassName();
thread.start();
}
}
Thank you!
9
0
4.5
6
class RunnableObject implements Runnable {
private Thread t;
private String threadName;
RunnableObject( String name) {
System.out.println("Creating " + threadName );
threadName = name;
t = new Thread (this, threadName);
}
public void run() {
System.out.println("Running " + threadName );
}
public void start () {
System.out.println("Starting " + threadName );
t.start ();
}
}
public class TestThread {
public static void main(String args[]) {
RunnableObject R1 = new RunnableObject( "Thread-1");
R1.start();
}
}
Thank you!
6
0
Are there any code examples left?
New code examples in category Java
-
Java 2022-03-27 21:35:04 Sort string array in case insensitive order and case sensitive order java
-
Java 2022-03-27 21:25:10 java -jar -l resources\es.porperties -i ejemplo.txt -o inject.bin
-
Java 2022-03-27 21:20:21 Debug & Fix a 2-Dimensional Array Java Console Application
-
Java 2022-03-27 20:40:19 TreeSet headSet(E toElement) method in java
-
Java 2022-03-27 19:30:06 close keyboard android
-
Java 2022-03-27 19:20:40 java measure execution time
-
Java 2022-03-27 19:10:06 how to add cardview support in android studio
-
Java 2022-03-27 18:55:08 android studio lower case letters on a button