0% found this document useful (0 votes)
5 views4 pages

Java Thread Priority and File Checks

Uploaded by

faadilkhaleel360
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)
5 views4 pages

Java Thread Priority and File Checks

Uploaded by

faadilkhaleel360
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

1. Write a program to define three threads with NORMAL, LOW and HIGH priority.

Run all three


threads to demonstrate the Thread Priority concept..
class MyThread extends Thread {
public MyThread(String name) {
super(name);
}

public void run() {


for (int i = 0; i < 5; i++) {
[Link]([Link]().getName() + " with priority " +
[Link]().getPriority() + " is running");
try {
[Link](100);
} catch (InterruptedException e) {
[Link](e);
}
}
}
}

public class ThreadPriorityExample {


public static void main(String[] args) {
// Creating threads
MyThread thread1 = new MyThread("Thread-1 (Low Priority)");
MyThread thread2 = new MyThread("Thread-2 (Normal Priority)");
MyThread thread3 = new MyThread("Thread-3 (High Priority)");

// Setting priorities
[Link](Thread.MIN_PRIORITY); // Low priority
[Link](Thread.NORM_PRIORITY); // Normal priority
[Link](Thread.MAX_PRIORITY); // High priority

// Starting threads
[Link]();
[Link]();
[Link]();
}
}

OutPut:
[Link] a Java program to check whether the fi le is readable, writable and hidden.

import [Link];

public class FileAttributesCheck {


public static void main(String[] args) {

String filePath = "[Link]";

File file = new File(filePath);

if ([Link]()) {
// Check if the file is readable
if ([Link]()) {
[Link]("The file is readable.");
} else {
[Link]("The file is not readable.");
}

if ([Link]()) {
[Link]("The file is writable.");
} else {
[Link]("The file is not writable.");
}

if ([Link]()) {
[Link]("The file is hidden.");
} else {
[Link]("The file is not hidden.");
}
} else {
[Link]("The file does not exist.");
}
}
}

OutPut:
[Link] a program that will count the number of characters and words in a file.

import [Link];
import [Link];
import [Link];

public class FileWordCharacterCount {


public static void main(String[] args) {

String filePath = "[Link]";

int wordCount = 0;
int characterCount = 0;

try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {


String line;

while ((line = [Link]()) != null) {

characterCount += [Link]();

String[] words = [Link]("\\s+");

wordCount += [Link];
}
} catch (IOException e) {
[Link]("An error occurred while reading the file: " + [Link]());
}

[Link]("Number of characters: " + characterCount);


[Link]("Number of words: " + wordCount);
}
}

You might also like