创建线程有两种方法:
1. 1.Thread类的子类创建线程,重写run()方法。然后就可以使用该类创建对象也就是一个线程,对象调用父类的start()方法线程开始执行。
2. 2.public final String getName()获得线程的名字。
3. public final void setDaemon(boolean on)设置线程为守护线程当系统没有其他线程运行 时及时守护线程没有运行完也要结束退出。
3.public static void sleep(long millis)方法设置线程休眠时间。
4.public static void yield()方法可以设置线程在该时间片内临时停止让其他线程执行。
5.public final void setPriority(int newPriority)方法设置线程的优先级从1-10优先级依次增高。
6.public static Thread currentThread()类方法可以获得当前正在执行线程。
7.通过实现Runnable接口创建线程。然后用Runnable接口的实现类创建对象MyThread mt=new MyThread();再用Thread类含有参数的构造方法去创建3个线程
new Thread(mt).start(); new Thread(mt).start(); new Thread(mt).start();因为这三个线程使用同一个对象创建的则都可以操作对象里面的成员变量。如多个代售点同时售票。
8.当然这种也可以使用内部类继承Thread类实现
class MyThread
{
int index=0;
private class InnerThread extends Thread
{
public void run()
{
while(true)
{
System.out.println(Thread.currentThread().getName()+":"+index++);
}
}
}
Thread getThread()
{
return new InnerThread();
}
}
在主函数中 MyThread mt=new MyThread()
mt.getThread().start();
mt.getThread().start();
mt.getThread().start();
mt.getThread().start();
因为每次调用mt.getThread().start();就会新创建一个内部类的对象也就是一个线程,又因为内部类可以操作外部类的变量和函数所以内部类产生的多个
线程对象改变的是同一个对象的成员变量。
9.线程的同步问题,当一段代码有一个线程进去执行时我们就不希望再有其他的线程进去执行,这时我们就需要对这部分代码加锁及当一个线程进去执行时就在这部分代
码前加上锁不让其他进程进来,当这个线程执行完出去时就把锁解开让其它线程进来执行。
10.同步块方法实现线程同步,用synchronized(Object obj){…}把要保护的代码包起来,obj可以是任何一个对象一般用this表示当前对象。
11.另一个实现线程同步的方法时用同步方法,也就是说将要进行同步的代码写成一个方法,再用synchronized关键字修饰这个方法也可以实现线程的同步。
12.静态方法进行同步时所用的锁是静态方法所在的类对应的Class对象的锁。
13.每一个对象除了有一个锁之外,还有一个等待队列(wait set),当对象刚创建时它的队列的是空的,
我们应该在当前线程锁住对象后,去调用该对象的wait方法把再来想进入同步块里面执行的进程放入等待队列。
当对象调用notify方法时,将将从对象的等待队列中删除一个任意选择的线程,这个线程将再次成为可运行的线程。
当调用对象的notifyAll方法时,将从该对象的等待队列中删除所有等待线程,这些线程将重新成为可运行线程。
Wait和notify主要用于producer-consumer(生产者-消费者)关系中。
且wait,notify,notifyAll方法都只能在同步块或同步方法中出现。这三个方法属于Object类的方法。
14. public static boolean interrupt()方法是Thread类的一个静态方法,当调用方法时可以是当前对象的线程中断,退出执行与
public static void yield()不同,yield方法只是让线程在这个时间片暂停让其它线程执行,当道下个时间片时就会继续执行原来的线程。
二.多个线程共享资源做不同的事。
1.共享资源以参数的形式传入到线程类中两个线程的Run方法分别实现不同的内容
生产者和消费者问题
class Producer implements Runnable
{
Q q;
public Producer(Q q)
{
this.q=q;
}
public void run()
{
int i=0;
while(true)
{
synchronized(q)
{
if(q.bfull)
try{q.wait();}catch(Exception e){System.out.println(e.getMessage());}
if(i==0)
{
q.name="李四";
try
{
Thread.sleep(2);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
q.sex="男";
System.out.println("put"+i);
}else
{
q.name="张三";
q.sex="女";
}
q.bfull=true;
q.notify();
}
i=(i+1)%2;
}
}
}
class Consumer implements Runnable
{
Q q;
public Consumer(Q q)
{
this.q=q;
}
public void run()
{
while(true)
{
synchronized(q)
{
if(!q.bfull)
try{q.wait();}catch(Exception e){System.out.println(e.getMessage());}
System.out.print(q.name);
System.out.println(q.sex);
q.bfull=false;
q.notify();
}
}
}
}
class Q
{
String name="unknown";
String sex="unknown";
boolean bfull=false;
}
class Test
{
public static void main(String[] args)
{
Q q=new Q();
new Thread(new Producer(q)).start();
new Thread(new Consumer(q)).start();
}
}
2.在共享资源所在的类中分别实现同步的不同操作,然后将资源对象分别传到不同的线程中,不同线程的run方法调用资源对象的不同方法,则不同线程执行不同方法。
public class ProducerConsumerTest {
public static void main(String[] args) {
Info info = new Info();
Producer p = new Producer(info);
Consumer c = new Consumer(info);
p.start();
c.start();
}
}
class Info {
String name = "unknow";
String sex = "unknow";
boolean isfull = false;
int i = 0;
public synchronized void put() {
if (this.isfull == false) {
if (i == 0) {
this.name = "张三";
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.sex = "男";
this.isfull = true;
} else {
this.name = "李四";
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.sex = "女";
this.isfull = true;
}
i = (i + 1) % 2;
notify();
} else {
try {
wait();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public synchronized void get() {
if (this.isfull == true) {
System.out.print(name + "------>");
name = "unknow";
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(sex);
sex = "unknow";
isfull = false;
notify();
} else {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Producer extends Thread {
Info info;
public Producer(Info info) {
this.info = info;
}
public void run() {
while (true) {
info.put();
}
}
}
class Consumer extends Thread {
Info info;
public Consumer(Info info) {
this.info = info;
}
public void run() {
while (true) {
info.get();
}
}
}