单线程情况
/**
* 测试单线程
* @author Toshiba
*
*/
public class TestThread {
public static void main(String[] args) {
new OneThread().run();//单线程的情况(后面要等前面执行完才能执行);
while(true){
System.out.println("main:"+Thread.currentThread().getName());
}
}
}
class OneThread{
public void run(){
while(true){
System.out.println("run:"+Thread.currentThread().getName());
}
}
}
实现一个线程和main线程
/**
* 测试(继承Thread)多线程
* @author Toshiba
*
*/
public class TestThreadTwo {
public static void main(String[] args) {
new TwoThread().start();//重写thread中的run方法,用子类的start去启用线程,调用子类的run方法。
while(true){
System.out.println("main:"+Thread.currentThread().getName());
}
}
}
class TwoThread extends Thread{
public void run(){
while(true){
System.out.println("run:"+Thread.currentThread().getName());
}
}
}
前台线程和后台线程的问题
/**
* 后台线程与前台线程的区别
* @author Toshiba
*
*/
public class TestThreadThree {
public static void main(String[] args) {
Thread tt = new ThreadThree();
tt.setDaemon(true);//设置为后台线程
tt.start();
//没有前台线程,后台线程将很快结束。
/*while(true){
System.out.println("main:"+Thread.currentThread().getName());
}*/
}
}
class ThreadThree extends Thread{
public void run(){
while(true){
System.out.println("run:"+Thread.currentThread().getName());
}
}
}
线程的合并和分开
/**
* 合并和分开线程
* @author Toshiba
*
*/
public class TestThreadFour {
//jdk1.7中的join()是合并后就合并,不会像1.4中那样合并后还能分开,或者是等到main执行100次后再合并,当然也有可能是机器的问题??
public static void main(String[] args) {
int index=0;
Thread tt = new FourThread();
tt.start();
while(true){
if(index==100){
try {
tt.join(10000);
System.out.println("main:"+Thread.currentThread().getName());
index++;
} catch (InterruptedException e) {e.printStackTrace();}
}
}
}
}
class FourThread extends Thread{
public void run(){
while(true){
System.out.println("run:"+Thread.currentThread().getName());
}
}
}
Runnable和Thread的区别
/**
* 区别thread和rannable
* @author Toshiba
*
*/
public class TestThreadFiveRunnable {
public static void main(String[] args) {
/*new ThreadFive().start();
new ThreadFive().start();
new ThreadFive().start();
new ThreadFive().start();*///extends Thread这种情况是4个对象各卖自己的100张票
/*ThreadFive tt = new ThreadFive();
tt.start();
tt.start();
tt.start();
tt.start();*/// extends Thread 这种方式只会启动一个线程
ThreadFive tt = new ThreadFive();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();// implements Runnable 这种方式会启动多个线程,比继承thread的方式来实现线程要灵活得多。
}
}
class ThreadFive implements Runnable /*extends Thread*/{
int tickets=100;
@Override
public void run() {
while(true){
if(tickets>0){
System.out.println("run:"+Thread.currentThread().getName()+"is saling tickets"+tickets--);
}
}
}
}
线程的同步问题
/**
* 多线程在实际中的应用,多线程聊天室,copy多条数据,中途停止,服务器。
* @author Toshiba
* 多线程同步。jdk1.7的版本已经做了很多同步的处理。
*/
public class TestThreadSix {
public static void main(String[] args) {
ThreadSix tt = new ThreadSix();
new Thread(tt).start();
tt.str = new String("method");
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
}
//多线程不同步的安全性问题run:Thread-1is saling ticket1 ,run:Thread-2is saling ticket0(但是票号不能打印出0,)
//synchronized通过检查标志位(锁题标)可以对代码进行线程同步。
}
class ThreadSix implements Runnable{
int tickets=100;
String str=new String("");//这个同步的锁对象,要放在run方法外(需要的是同一个对象)
@Override
public void run() {
if(str.equals("method")){//检查的是str="method"对象。
while(true){
synchronized (str) {
if(tickets>0){
try {Thread.sleep(100L);} catch (InterruptedException e) {e.printStackTrace();}
System.out.println("run:"+Thread.currentThread().getName()+"is saling ticket"+tickets--);
}
}
}
}else{
while(true){
sale();
}
}
}
//可以是同步的函数(检查的是this对象)
public synchronized void sale(){
if(tickets>0){
try {Thread.sleep(100L);} catch (InterruptedException e) {e.printStackTrace();}
System.out.println(Thread.currentThread().getName()+"is saling ticket"+tickets--);
}
}
}