1: How to Create Threads in Java by Extending Thread Class
class MyClass extends Thread{
public void run(){
for(int i = 0; i<10; i++)
System.out.println(Thread.currentThread().getId() + " Value: " + i);
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class JavaThreadExample {
public static void main(String[] args) {
// TODO code application logic here
MyClass mc = new MyClass();
mc.start();
MyClass mc1 = new MyClass();
mc1.start();
}
}
2: Creating Java Threads by implementing Runnable Interface
class MyClass implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getId() + " Value: " + i);
}
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class RunnableExample {
public static void main(String[] args) {
Thread t1 = new Thread(new MyClass());
Thread t2 = new Thread(new MyClass());
t1.start();
t2.start();
// How to create thread inside main class
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getId() + " Value: " + i);
}
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
t3.start();
}
}
3: Java Thread.join() Method and Synchronized Method
public class MyThreadJoin {
private static int count = 0;
public synchronized static void counter() {
count++;
}
public static void main(String[] args) {
try {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 10000; i++) {
counter();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 10000; i++) {
counter();
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Value:" + count);
} catch (InterruptedException ex) {
Logger.getLogger(MyThreadJoin.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class MyClass extends Thread{
public void run(){
for(int i = 0; i<10; i++)
System.out.println(Thread.currentThread().getId() + " Value: " + i);
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class JavaThreadExample {
public static void main(String[] args) {
// TODO code application logic here
MyClass mc = new MyClass();
mc.start();
MyClass mc1 = new MyClass();
mc1.start();
}
}
2: Creating Java Threads by implementing Runnable Interface
class MyClass implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getId() + " Value: " + i);
}
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class RunnableExample {
public static void main(String[] args) {
Thread t1 = new Thread(new MyClass());
Thread t2 = new Thread(new MyClass());
t1.start();
t2.start();
// How to create thread inside main class
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getId() + " Value: " + i);
}
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
t3.start();
}
}
3: Java Thread.join() Method and Synchronized Method
public class MyThreadJoin {
private static int count = 0;
public synchronized static void counter() {
count++;
}
public static void main(String[] args) {
try {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 10000; i++) {
counter();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 10000; i++) {
counter();
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Value:" + count);
} catch (InterruptedException ex) {
Logger.getLogger(MyThreadJoin.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
No comments:
Post a Comment