Uploaded by Savita Gondi

Java Package Tutorial: Create and Use Packages

advertisement
1. Write Your Java Code
2. Open Notepad.
3. Write your Java code and include the package declaration at the
top.
Example:
package mypack;
public class MyClass {
public void displayMessage() {
System.out.println("Hello from MyClass in the package
mypack!");
}
}
3. Save the file:
o Save the file with the name MyClass.java.
o Create a folder for your package (e.g., mypack).
o Save the file inside the mypack folder.
Example: C:\JavaProjects\mypack\MyClass.java.
2. Compile the Java Code
1. Open the Command Prompt:
o Navigate to the parent directory of mypack.
Example:
o
javac mypack\MyClass.java
3. Write a Program to Use the Package
1. Open Notepad again and write a program that uses the mypack
package.
Example:
import mypack.MyClass;
public class MainApp {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.displayMessage();
}
}
2. Save this file:
o Save it in the parent directory (not inside the mypack folder).
o Name the file MainApp.java.
Example: C:\JavaProjects\MainApp.java.
4. Compile and Run the Main Program
1. Open the Command Prompt and navigate to the parent directory:

cd C:\JavaProjects
2. Compile the MainApp.java file:
javac MainApp.java
2. Run: java MainApp
1. Class MyThread
This class extends Thread and customizes its behavior.

Constructor:
public MyThread(String name) {
super(name); // Call the parent Thread class constructor to set the thread name
start();
// Start the thread
}


The super(name) call initializes the thread with the specified name.
start() is invoked to launch the thread and schedule it for execution.
 run Method:
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " Count: " + i);
try {
Thread.sleep(500); // Pause the thread for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " Thread interrupted.");
}
}
}
 This method contains the code that the thread executes when started.
 It loops 5 times, printing the current thread's name and count.
 Between iterations, the thread sleeps for 500 milliseconds (Thread.sleep(500)), simulating a
delay.
 If interrupted, the thread catches the InterruptedException and prints a message.
2. Class ThreadConcurrentExample (Main Class)
This is the driver class where the program starts execution.

main Method:
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.");
}
}
}
 A new thread is created by instantiating the MyThread class, naming it "Child Thread".
 When the MyThread constructor is called, the start() method is invoked automatically,
which begins executing the run method in a separate thread.
 Simultaneously, the main thread executes its own loop, printing the current thread's name
(main) and count, also with a delay of 500 milliseconds between iterations.
How the Program Works
1. Thread Creation:
o When MyThread is instantiated, a new thread (Child Thread) starts and runs
concurrently with the main thread.
2. Concurrency:
o The main thread and the child thread run independently of each other.
3. Thread Output:
o Both threads print messages (Thread Name + Count) 5 times, pausing for 500
milliseconds after each iteration.
Download