java thread
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();
}
}
java thread
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();
}
}
multithreading in java
class Count implements Runnable
{
Thread mythread ;
Count()
{
mythread = new Thread(this, "my runnable thread");
System.out.println("my thread created" + mythread);
mythread.start();
}
public void run()
{
try
{
for (int i=0 ;i<10;i++)
{
System.out.println("Printing the count " + i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("my thread interrupted");
}
System.out.println("mythread run is over" );
}
}
class RunnableExample
{
public static void main(String args[])
{
Count cnt = new Count();
try
{
while(cnt.mythread.isAlive())
{
System.out.println("Main thread will be alive till the child thread is live");
Thread.sleep(1500);
}
}
catch(InterruptedException e)
{
System.out.println("Main thread interrupted");
}
System.out.println("Main thread run is over" );
}
}
threads java
// 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();
}
}
}
create thread java
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();
}
}
java thread
public static void main(String[] args {
...
Thread t1= new Thread(...);
t1.start();
...
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us