java threads

35
1

Upload: pendekanti-surendra-kumar

Post on 30-Mar-2016

243 views

Category:

Documents


0 download

DESCRIPTION

Java Threads

TRANSCRIPT

Page 1: Java Threads

1

Page 2: Java Threads

2

Page 3: Java Threads

3

Page 4: Java Threads

Getting the information about “main” thread. The “main” Thread is a Non-Daemon thread and will be alive until the main() method is executing. Once main() method is pop out of stack the “main” thread is dead.

Thread.currentThread() returns a reference to the currently executing thread object, in this case it is the “main” thread

Thread information when printed using toString() returns :Thread[ThreadGroup, PRIORITY, ThreadName]

4

Page 5: Java Threads

5

Page 6: Java Threads

6

Page 7: Java Threads

1) Once a block of data has been read, the computation process can start, and as soon as the computation

has been completed, the results can be written out.

2) If one thread is dependent on another you can’t afford to have one overtaking the other—otherwise, you’ll have chaos. This needs inter thread communication.

3) You could also organize the program so that reading a block from the file is one activity, performing

the calculation is a second activity, and writing the results is a third activity.

7

Page 8: Java Threads

8

Page 9: Java Threads

9

Page 10: Java Threads

10

Page 11: Java Threads

11

Page 12: Java Threads

12

Page 13: Java Threads

13

Page 14: Java Threads

14

Page 15: Java Threads

Note: Each Thread has a separate stack, but all the threads are going to share heap area( instance variables ) and Method area( static variables).

15

Page 16: Java Threads

16

Page 17: Java Threads

17

Page 18: Java Threads

18

Page 19: Java Threads

19

Page 20: Java Threads

20

Page 21: Java Threads

Answer: The Thread starts, but since the TestThread class has not overridden the run() method, the thread has no task to execute. The default run() provided by the Thread class is executed and the thread dies.

21

Page 22: Java Threads

Answer:

We should invoke start() method of Thread class in order to run as a separate unit of work.

Now since we are calling directly the run() method, it is a method call in main unit of work.

It works like a normal method call.

Output will be “One One” followed by “Two Two”.

22

Page 23: Java Threads

Answer: option 1

23

Page 24: Java Threads

24

Page 25: Java Threads

25

Page 26: Java Threads

26

Page 27: Java Threads

27

Page 28: Java Threads

28

Page 29: Java Threads

29

Page 30: Java Threads

30

Page 31: Java Threads

31

Page 32: Java Threads

32

Page 33: Java Threads

33

Page 34: Java Threads

34

Page 35: Java Threads

35