在实际应用中我们不会在一个手机或者电脑上进行单线程的开发或者使用,一般都是多线程。
线程:程序中执行的具体事务
进程:表示正在运行的应用程序,一个进程可以有多个线程。
事实上线程是有执行顺序的,但是由于CPU执行的速度非常快,所以觉得是在并发执行,其实是伪装的并发执行。
线程的实现有两种方法:继承Thread类,实现Runnable接口
具体代码实现如下:
public class Mythread {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//获取当前线程
//实现
Thread current = Thread.currentThread();
// first f = new first();
// f.start();
// f.setName("你说");
// second s = new second();
// s.start();
// s.setName("他说");
//实现接口的对象,自身不带任何线程处境(不开辟新线程)
//需要将实现接口的对象放入一个线程当中
RThread r = new RThread();
Thread th = new Thread(r);
th.start();
System.out.println("马上下课");
}
}
class first extends Thread
{
//重写父类的run方法
public void run()
{
System.out.println("hhhhh");
System.out.println("当前线程的名字:"+this.getName());
}
}
class second extends Thread
{
public void run()
{
System.out.println("hhhh");
System.out.println("当前线程的名字:"+this.getName());
}
}
class RThread implements Runnable
{
public void run()
{
System.out.println("正在上课中" + Thread.currentThread().getId());
try
{
Thread.sleep(1000);
}catch(Exception e)
{
System.err.println("当前线程被打断");
e.printStackTrace();
}
}
}
一般在线程中我们有时会在进行一个线程的时候,可能会插入另一个线程,一般使用join方法。下面用join方法实现插入线程的代码,结果为依次执行线程12345。
public class Threadjoin {
/**
* java多线程的join方法 插队现象
* 有a,b两个线程,a在b线程里调用了join方法,那么b就会暂停执行,先将a执行完再执行b
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// B b = new B();
// A a = new A(b);
// a.start();
// b.start();
//有五个线程1-5,希望按照12345执行
_1 a = new _1();
_2 b = new _2(a);
_3 c = new _3(b);
_4 d = new _4(c);
_5 e = new _5(d);
a.start();
b.start();
c.start();
d.start();
e.start();
}
}
class A extends Thread
{
//将B对象传入A中,并调用A方法
private B bThread;
public A(B bThread)
{
this.bThread = bThread;
}
public void run()
{
System.out.println("已经进入A线程");
System.out.println("A线程调用了B线程");
try
{
// 当前线程会发生阻塞,优先调用join方法
this.bThread.join();
}catch(Exception e)
{
e.printStackTrace();
}
System.out.println("A线程执行完毕");
}
}
class B extends Thread
{
public void run()
{
System.out.println("B线程正在执行");
try
{
Thread.sleep(2000);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
class _1 extends Thread
{
public void run()
{
try
{
System.out.println("线程1");
}catch(Exception e)
{
e.printStackTrace();
}
}
}
class _2 extends Thread
{
private _1 th1;
public _2(_1 th1)
{
this.th1 = th1;
//System.out.println("线程2");
}
public void run()
{
try
{
//线程1调用join方法
this.th1.join();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("线程2");
}
}
class _3 extends Thread
{
private _2 th2;
public _3(_2 th2)
{
this.th2 = th2;
//System.out.println("线程3");
}
public void run()
{
try
{
this.th2.join();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("线程3");
}
}
class _4 extends Thread
{
private _3 th3;
public _4(_3 th3)
{
this.th3 = th3;
//System.out.println("线程4");
}
public void run()
{
try
{
this.th3.join();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("线程4");
}
}
class _5 extends Thread
{
private _4 th4;
public _5(_4 th4)
{
this.th4 = th4;
//System.out.println("线程5");
}
public void run()
{
try
{
this.th4.join();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("线程5");
}
}
join()方法也称插队现象,当一个线程正在运行,另外一个线程调用了join()方法了之后,那么这个线程会停下来去执行那个插队的线程,等插队线程执行完了之后才会执行那个被打断的程序。