0% found this document useful (0 votes)
4 views5 pages

program10,11,12 (1)

The document provides Java code examples for creating a package named 'mypack' with a class 'MyPackageClass' that includes methods for displaying a message and adding numbers. It also illustrates thread creation using the Runnable interface and a custom thread class 'MyThread' that demonstrates concurrent execution with the main thread. Instructions for organizing files and compiling the programs are included.

Uploaded by

niwic12695
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

program10,11,12 (1)

The document provides Java code examples for creating a package named 'mypack' with a class 'MyPackageClass' that includes methods for displaying a message and adding numbers. It also illustrates thread creation using the Runnable interface and a custom thread class 'MyThread' that demonstrates concurrent execution with the main thread. Instructions for organizing files and compiling the programs are included.

Uploaded by

niwic12695
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

10.

Develop a JAVA program to create a package named mypack and import & implement it
in a suitable class.

Package mypack

// Inside a folder named 'mypack'

package mypack;

public class MyPackageClass {

public void displayMessage() {

System.out.println("Hello from MyPackageClass in mypack package!");

// New utility method

public static int addNumbers(int a, int b) {

return a + b;

Now, let’s create the main program in a different file outside

the mypack folder:

PackageDemo class using mypack Package

// Main program outside the mypack folder

import mypack.MyPackageClass;

//import mypack.*;

public class PackageDemo {

public static void main(String[] args) {

// Creating an instance of MyPackageClass from the mypack package

MyPackageClass myPackageObject = new MyPackageClass();

// Calling the displayMessage method from MyPackageClass

myPackageObject.displayMessage();

// Using the utility method addNumbers from MyPackageClass


int result = MyPackageClass.addNumbers(5, 3);

System.out.println("Result of adding numbers: " + result);

To compile and run this program, you need to follow these steps:

Organize your directory structure as follows:

project-directory/

├── mypack/

│ └── MyPackageClass.java

└── PackageDemo.java

Compile the files:

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).

class MyRunnable implements Runnable {

private volatile boolean running = true;

@Override

@SuppressWarnings("deprecation")

public void run() {

while (running) {

try {

// Suppress deprecation warning for Thread.sleep()

Thread.sleep(500);

System.out.println("Thread ID: " + Thread.currentThread().getId() + "

is running.");

} catch (InterruptedException e) {
System.out.println("Thread interrupted.");

public void stopThread() {

running = false;

public class RunnableThreadExample {

public static void main(String[] args) {

// Create five instances of MyRunnable

MyRunnable myRunnable1 = new MyRunnable();

MyRunnable myRunnable2 = new MyRunnable();

MyRunnable myRunnable3 = new MyRunnable();

MyRunnable myRunnable4 = new MyRunnable();

MyRunnable myRunnable5 = new MyRunnable();

// Create five threads and associate them with MyRunnable instances

Thread thread1 = new Thread(myRunnable1);

Thread thread2 = new Thread(myRunnable2);

Thread thread3 = new Thread(myRunnable3);

Thread thread4 = new Thread(myRunnable4);

Thread thread5 = new Thread(myRunnable5);

// Start the threads

thread1.start();

thread2.start();

thread3.start();

thread4.start();
thread5.start();

// Sleep for a while to allow the threads to run

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

// Stop the threads gracefully

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.

class MyThread extends Thread {

// Constructor calling base class constructor using super

public MyThread(String name) {

super(name);

start(); // Start the thread in the constructor

// The run method that will be executed when the thread starts

@Override

public void run() {


for (int i = 1; i <= 5; i++) {

System.out.println(Thread.currentThread().getName() + " Count: " + i);

try {

Thread.sleep(500); // Sleep for 500 milliseconds

} catch (InterruptedException e) {

System.out.println(Thread.currentThread().getName() + " Thread

interrupted.");

public class ThreadConcurrentExample {

public static void main(String[] args) {

// Create an instance of MyThread

MyThread myThread = new MyThread("Child Thread");

// Main thread

for (int i = 1; i <= 5; i++) {

System.out.println(Thread.currentThread().getName() + " Thread Count:

" + i);

try {

Thread.sleep(500); // Sleep for 500 milliseconds

} catch (InterruptedException e) {

System.out.println(Thread.currentThread().getName() + " Thread

interrupted.");

}}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy