Java - A Simple Thread (Sample Code)
1 : A simple thread
package thanhcs.blogspot.com;
public class ASimpleThread extends Thread{
private int total;
private String threadNdame;
public ASimpleThread(int total, String threadName){
this.total = total;
this.threadNdame = threadName;
}
@Override
public void run() {
for(int i = 0 ;i < total ; i++){
System.out.println("Thread name : "+threadNdame + " -- "+i);
}
super.run();
}
public static void main(String[] args) {
ASimpleThread a = new ASimpleThread(3, "Thread");
ASimpleThread b = new ASimpleThread(10, "Another Thread");
a.start();
b.start();
}
}
Result 1 :
Thread name : Thread -- 0
Thread name : Thread -- 1
Thread name : Another Thread -- 0
Thread name : Another Thread -- 1
Thread name : Thread -- 2
Thread name : Another Thread -- 2
Thread name : Another Thread -- 3
Thread name : Another Thread -- 4
Thread name : Another Thread -- 5
Thread name : Another Thread -- 6
Thread name : Another Thread -- 7
Thread name : Another Thread -- 8
Thread name : Another Thread -- 9
2: A simple Runable Thread
package thanhcs.blogspot.com;
public class ASimpleRunableThread implements Runnable {
private int total;
private String threadName;
public ASimpleRunableThread(int total, String threadName) {
super();
this.total = total;
this.threadName = threadName;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0 ; i <total ; i++){
System.out.println("Thread name : "+threadName + " -- "+i);
}
}
public static void main(String[] args) {
Thread b = new Thread(new ASimpleRunableThread(10, "Another Thread"));
Thread a = new Thread(new ASimpleRunableThread(3, "Thread"));
b.start();
a.start();
}
}
Result 2 : Same as Result 1
3: Thread priority
constants defiend in Thread class:
|
public static void main(String[] args) {
Thread a = new Thread(new ASimpleRunableThread(3, "Thread"));
Thread b = new Thread(new ASimpleRunableThread(10, "Another Thread"));
a.setPriority(Thread.MAX_PRIORITY);
b.setPriority(Thread.MIN_PRIORITY);
System.out.println("get Priority : Thread a :"+ a.getPriority() +" Thread b : "+b.getPriority());
a.start();
b.start();
}
Result 3: Some thing change , right :3
4: Join()
add join() function to thread a and see what happend :)
code :
public static void main(String[] args) throws Exception{
Thread a = new Thread(new ASimpleRunableThread(3, "Thread"));
Thread b = new Thread(new ASimpleRunableThread(10, "Another Thread"));
a.setPriority(Thread.MAX_PRIORITY);
b.setPriority(Thread.MIN_PRIORITY);
a.start();
b.start();
a.join();
System.out.println("get Priority : Thread a :"+ a.getPriority() +" Thread b : "+b.getPriority());
}
Result 4:
Thread name : Thread -- 0
Thread name : Thread -- 1
Thread name : Thread -- 2
Thread name : Another Thread -- 0
get Priority : Thread a :10 Thread b : 1
Thread name : Another Thread -- 1
Thread name : Another Thread -- 2
Thread name : Another Thread -- 3
Thread name : Another Thread -- 4
Thread name : Another Thread -- 5
Thread name : Another Thread -- 6
Thread name : Another Thread -- 7
Thread name : Another Thread -- 8
Thread name : Another Thread -- 9
5 : Staring and Stop Thread
package thanhcs.blogspot.com;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DigitalTest {
public static void main(String[] args) throws Exception{
DigitalClock d1, d2;
Thread dc1 = new Thread(d1 = new DigitalClock("VietNam", 0));
Thread dc2 = new Thread(d2 = new DigitalClock("Japan", 3));
dc1.start();
dc2.start();
Thread.sleep(5000);
d1.StopClock();
d2.StopClock();
}
}
class DigitalClock implements Runnable{
private boolean isRun = true;
private Calendar clock;
private String town;
public DigitalClock(String town, int different) {
super();
this.town = town;
clock = Calendar.getInstance();
clock.add(Calendar.HOUR, different);
}
@Override
public void run() {
// TODO Auto-generated method stub
SimpleDateFormat timeformat = new SimpleDateFormat("hh:mm:ss a");
while(isRun){
clock.add(Calendar.SECOND, 1); // advance clock
System.out.println(town +" = "+ timeformat.format(clock.getTime()));
try{
Thread.sleep(1000);
}catch(Exception e){
}
}
}
public void StopClock()
{
isRun = false;
}
}
Result 5:
Japan = 05:14:25 AM
VietNam = 02:14:25 AM
Japan = 05:14:26 AM
VietNam = 02:14:26 AM
Japan = 05:14:27 AM
VietNam = 02:14:27 AM
Japan = 05:14:28 AM
VietNam = 02:14:28 AM
Japan = 05:14:29 AM
VietNam = 02:14:29 AM
6 : Coordianting threads
wait();
notify();
notifyAll();
7 : Synchronising threads.
No comments: