program10,11,12 (1)
program10,11,12 (1)
Develop a JAVA program to create a package named mypack and import & implement it
in a suitable class.
Package mypack
package mypack;
return a + b;
import mypack.MyPackageClass;
//import mypack.*;
myPackageObject.displayMessage();
To compile and run this program, you need to follow these steps:
project-directory/
├── mypack/
│ └── MyPackageClass.java
└── PackageDemo.java
javac mypack/MyPackageClass.java
javac PackageDemo.java
11. Write a program to illustrate creation of threads using runnable class. (start method start
each of the newly created thread. Inside the run method there is sleep() for suspend the thread
for 500 milliseconds).
@Override
@SuppressWarnings("deprecation")
while (running) {
try {
Thread.sleep(500);
is running.");
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
running = false;
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
myRunnable1.stopThread();
myRunnable2.stopThread();
myRunnable3.stopThread();
myRunnable4.stopThread();
myRunnable5.stopThread();
12. Develop a program to create a class MyThread in this class a constructor, call the base
class constructor, using super and start the thread. The run method of the class starts after
this. It can be observed that both main thread and created child thread are executed
concurrently.
super(name);
// The run method that will be executed when the thread starts
@Override
try {
} catch (InterruptedException e) {
interrupted.");
// Main thread
" + i);
try {
} catch (InterruptedException e) {
interrupted.");
}}