-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadExample.java
More file actions
63 lines (50 loc) · 1.41 KB
/
ThreadExample.java
File metadata and controls
63 lines (50 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ThreadExample {
public static void main(String[] args) {
Thread3 thread1 = new Thread3("Hello.txt");
thread1.start();
Thread3 thread2 = new Thread3("randtxt.txt");
thread2.start();
}
}
class Thread3 implements Runnable {
Thread thread;
private String fileName;
Thread3(String threadname) {
fileName = threadname;
}
public void run() {
for(int i = 0; i < 2; i++)
{
System.out.println(i);
System.out.println(fileName);
try
{
File file = new File(fileName);
Scanner scan1 = new Scanner(file);
while (scan1.hasNextLine()) // loop through file and display contents
{
String input = scan1.nextLine();
System.out.println("From file: " + input);
}
}
catch(FileNotFoundException e)
{
System.out.println("File not found!"); // file not found exception
}
}
}
public void start() {
System.out.println("Thread started");
/*taking an if condition to check whether class variable thread
has value in it or no. If its null then we are creating an instance using
thread class which takes the name as a parameter (value for which was assigned
in the constructor). After which the thread is started using start() method.*/
if (thread == null) {
thread = new Thread(this, fileName);
thread.start();
}
}
}