Untitled
unknown
plain_text
2 years ago
1.6 kB
6
Indexable
10
package mypack;
public class MyPackageClass {
public void display() {
System.out.println("Hello from MyPackageClass in mypack package!");
}
}
package Programs;
import mypack.MyPackageClass;
public class Main {
public static void main(String[] args) {
MyPackageClass myPackageObj = new MyPackageClass();
myPackageObj.display();
}
}
11
class MyRunnable implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread " + Thread.currentThread().getId() + ": " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
MyRunnable myRunnable1 = new MyRunnable();
MyRunnable myRunnable2 = new MyRunnable();
Thread thread1 = new Thread(myRunnable1);
Thread thread2 = new Thread(myRunnable2);
thread1.start();
thread2.start();
}
}
12
package Programs;
class MyThread extends Thread {
public MyThread(String name) {
super(name);
start();
}
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + ": " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Main Thread: " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
MyThread myThread = new MyThread("Child Thread");
}
}
Editor is loading...
Leave a Comment