Multithreading 1
Multithreading 1
In online session, what are the different activities done by students as?
- Listen the class
- Taking running notes
- Checking mobile
Process based-
1. Executing several tasks simultaneously where each task is separate
independent process such as multitasking is called as process based.
2. Example 1- Typing java program into eclipse, also listening the audio
songs, download a file from internet.
3. In this every activity is independent process here.
4. Example-2 Task manager, see the multiple process list.(Control+Shift+Esc
key)
5. Process is heavy weight components.
6. Each process has own address into memory.
Thread based-
1. Executing several tasks simultaneously where each task is separate part
of same program called as thread based.
2. Example- suppose I have 1000 lines of code into java program and it will
takes 4 hours to execute it where first 500 line is executed after that
remaining 500 lines is executed but there is no any dependency between
them so I can run that tasks simultaneously to minimize the execution
time.
3. Thread is light weight components.
4. Thread shares the same address space.
What is thread?
It is the smallest unit of program called as Thread.
Constructors
o Thread()
o Thread(String name)
o Thread(Runnable r)
o Thread(Runnable r,String name)
Methods-
package com.threads;
Program-1
package com.threads;
@Override
public void run() {
}
}
Note- If you are not extending the Thread class,your class object would not be
treated as a thread object.So you need to explicitely create Thread class
object.We are passing the object of your class that implements Runnable so that
your class run() method may execute.
When?
Extending thread class- if the class is not extending another class then we
should go for thread class.
package com.multi;
Output-
1
1
2
2
3
3
4
4
5
5
The users are not blocked because threads are independent, and we can
perform multiple operations at times
Program-3
package com.threads;
}
}
Output-
12
Thread-0
5
NEW