------- android培训、java培训、期待与您交流! ----------
线程 是程序中的执行线程。Java 虚拟机允许应用程序并发地运行多个执行线程。
每个线程都有一个优先级,高优先级线程的执行优先于低优先级线程。每个线程都可以或不可以标记为一个守护程序。当某个线程中运行的代码创建一个新 Thread
对象时,该新线程的初始优先级被设定为创建线程的优先级,并且当且仅当创建线程是守护线程时,新线程才是守护程序。
当 Java 虚拟机启动时,通常都会有单个非守护线程(它通常会调用某个指定类的 main
方法)。Java 虚拟机会继续执行线程,直到下列任一情况出现时为止:
- 调用了
Runtime
类的exit
方法,并且安全管理器允许退出操作发生。 - 非守护线程的所有线程都已停止运行,无论是通过从对 run 方法的调用中返回,还是通过抛出一个传播到
run
方法之外的异常。
创建新执行线程有两种方法。一种方法是将类声明为 Thread
的子类。该子类应重写 Thread
类的 run
方法。接下来可以分配并启动该子类的实例。例如,计算大于某一规定值的质数的线程可以写成:
class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } }
然后,下列代码会创建并启动一个线程:
PrimeThread p = new PrimeThread(143); p.start();
创建线程的另一种方法是声明实现 Runnable
接口的类。该类然后实现 run
方法。然后可以分配该类的实例,在创建 Thread
时作为一个参数来传递并启动。采用这种风格的同一个例子如下所示:
class PrimeRun implements Runnable { long minPrime; PrimeRun(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } }
然后,下列代码会创建并启动一个线程:
PrimeRun p = new PrimeRun(143); new Thread(p).start();
每个线程都有一个标识名,多个线程可以同名。如果线程创建时没有指定标识名,就会为其生成一个新名称。
class Demo extends Thread
{
public void run()
{
for (int x=0;x<60;x++)
System.out.println("demo run--"+x);
}
}
class ThreadDemo
{
public static void main(String[] args)
{
Demo d = new Demo();//创建好了一个线程
d.start();//开启线程,并调用该线程的run方法
//d.run();//仅仅是调用了对象方法,线程没有启动,是主线程的动作
for (int x = 0;x < 60;x++ )
{
System.out.println("Hello World!--"+x);
}
}
}
class Test extends Thread
{
// private String name;
Test(String name)
{
//this.name = name;//构造函数和成员变量不能在成员方法中,是独立存在的
super(name);
}
public void run()
{
for (int x= 0;x < 60; x++)
{
System.out.println((Thread.currentThread()==this)+"--test--"+x+"--"+this.getName());
}
}
}
class ThreadTest
{
public static void main(String[] args)
{
Test t1 = new Test("one--");
Test t2 = new Test("two++");
t1.start();
t2.start();
for (int x = 0;x < 60; x++)
{
System.out.println("--main--" + x);
}
}
}

class Ticket implements Runnable
{
private //static int tick = 100;
public void run()
{
while (true)
{
if (tick > 0)
System.out.println("sale : "+ tick--);
}
}
}
class TicketDemo
{
public static void main(String[] args)
{
Ticket t = new Ticket();/该对象非线程
Thread t1=new Thread(t);
Thread t2=new Thread(t);
Thread t3=new Thread(t);
Thread t4=new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();//这四个对象此时调用start方法调用的run方法,调用的Ticket类的对象只有一个,就是Runnable类接口的子类所创建的Ticket类对象t
}
}
class Ticket implements Runnable
{
private int tick = 10000;
Object obj = new Object();
public void run()
{
while (true)
{
synchronized (obj)
{
if (tick > 0)
try
{
Thread.sleep(10);
}
catch (Exception e)
{
}//这个异常不能抛,因为是接口类型
System.out.println(Thread.currentThread().getName()+"sale : "+ tick--);
}
}
}
}
class TicketDemo2
{
public static void main(String[] args)
{
Ticket t = new Ticket();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
Thread t3=new Thread(t);
Thread t4=new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Bank
{
private int sum;
Object obj = new Object();
public synchronize//或则 void add(int n)
{
synchronized(obj)
{
sum = sum + n;
try
{
Thread.sleep(10);
}
catch (Exception e)
{
}//这个函数可以抛出
System.out.println("sum="+sum);
}
}
}
class Cus implements Runnable
{
private Bank b = new Bank();
public void run()
{
for (int x = 0; x < 3; x++)
{
b.add(100);
}
}
}
class BankDemo
{
public static void main(String[] args)
{
Cus c = new Cus();
Thread t1 = new Thread(c);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}
class Ticket implements Runnable
{
private int tick = 100;
boolean flag = true;
public void run()
{
if (flag)
{
while (true)
{
synchronized(this)
{
if(tick > 0)
{
try{Thread.sleep(10);}catch (Exception e){}
System.out.println(Thread.currentThread().getName()+"--code--"+tick--);
}
}
}
}
else
while (true)
show();
}
public synchronized void show()
{
if (tick > 0)
{
try{Thread.sleep(10);}catch (Exception e){}
System.out.println(Thread.currentThread().getName()+"--show--"+tick--);
}
}
}
class ThislockDemo
{
public static void main(String[] args)
{
Ticket t = new Ticket();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.start();
try{Thread.sleep(10);}catch (Exception e){}
t.flag = false;
t2.start();
}
}
class Ticket implements Runnable
{
private static int tick = 100;
//Object obj = new Object();
boolean flag = true;
public void run()
{
if (flag)
{
while (true)
{
synchronized(Ticket.class)
{
if(tick > 0)
{
try{Thread.sleep(10);}catch (Exception e){}
System.out.println(Thread.currentThread().getName()+"--code--"+tick--);
}
}
}
}
else
while (true)
show();
}
public static synchronized void show()
{
if (tick > 0)
{
try{Thread.sleep(10);}catch (Exception e){}
System.out.println(Thread.currentThread().getName()+"--show--"+tick--);
}
}
}
class StaticMethodDemo
{
public static void main(String[] args)
{
Ticket t = new Ticket();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.start();
try{Thread.sleep(10);}catch (Exception e){}
t.flag = false;
t2.start();
}
}
class Single//懒汉式
{
private static Single s = null;
private Single(){}
public static //synchronized Single getInstance()//可以用同步函数或者同步代码块解决多线程访问的安全问题,但是相比较同步代码块效率更高
{
if (s==null)//多线程访问时会存在安全问题,需要同步
{
synchronized(Single.class)// 什么意思?
{ if(s==null)
s = new Single();
}
}
retrun s;
}
}
//饿汉式
class Single
{
private static final Single s = new Single();
private Single(){}
public static getInstance()
{
return s;
}
}
*/class Ticket implements Runnable
{
private int tick = 100;
boolean flag = true;
Object obj = new Object();
public void run()
{
if (flag)
{
while (true)
{
synchronized(obj)
{
show();
}
}
}
else
while (true)
show();
}
public synchronized void show()
{
synchronized(obj)
{
if(tick > 0)
{
try{Thread.sleep(10);}catch (Exception e){}
System.out.println(Thread.currentThread().getName()+"--code--"+tick--);
}
}
}
}
class DeadLockDemo
{
public static void main(String[] args)
{
Ticket t = new Ticket();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.start();
try{Thread.sleep(10);}catch (Exception e){}
t.flag = false;
t2.start();
}
}
class Test implements Runnable
{
private boolean flag;
Test(boolean flag)
{
this.flag=flag;
}
public void run();
{
if(flag)
{ while(true)
{
synchronized(locka)
{
System.out.println("if lcoka ");
synchronized(lockb)
{
System.out.println("if lockb");
}
}
}
}
else()
{
synchronized(lockb)
{
System.out.println("else lockb");
synchronized(locka)
{
System.out.println("else locka");
}
}
}
}
}
class MyLock
{
Object locka=new Object();
Object lockb=new Object();
}
class
{
public static void main(String[] args)
{
Thread t1 = new Thread(new Test(true));
Thread t2 = new Thread(new Test(false));
t1.start();
t2.start();
}
}