Java 线程知识

来源:互联网 发布:mac 触摸板 鼠标方向 编辑:程序博客网 时间:2024/04/30 14:45
java 牛逼的就是对多线程的处理,先贴点概念性的东西,可能不准确。

1.线程的基本概念、线程的基本状态及状态之间的关系
  线程就是应用程序中的一个可单独执行的,完成某种操作的程序块。
  多线程就是同一个应用程序中有多个可执行的程序块,它们可以并发执行。
状态:a.就绪,b.运行,c.synchronize阻塞,d.wait和sleep挂起,e.结束。wait必须在synchronized内部调用。
对线程进行控制:    
  start():用于调用run()方法使线程开始执行。
  stop(): 立即停止线程执行,其内部状态清零,放弃占用资源。
  suspend():暂停线程执行。线程的所有状态和资源保持不变,以后可以通过另一线程调用resume()方法来重新启动这个线程。 
  resume():恢复暂停的线程,安排暂停线程执行。  
  sleep(): 调整Java执行时间,所需参数是指定线程的睡眠时间,以毫秒为单位。
  join():调用线程等待本线程执行结束。
  yield():暂停调度线程并将其放在等待队列末尾,等待下一轮执行,使同优先级的 其它线程有机会执行 。 
  isAlive():判断线程目前是否正在执行状态中


2.线程与进程的区别?
  线程是指进程内的一个执行单元,也是进程内的可调度实体,一个进程中有很多个线程。
区别:
   (1)地址空间:进程内的一个执行单元;进程至少有一个线程;它们共享进程的地址空间;而进程有自己独立的地址空间;
   (2)资源拥有:进程是资源分配和拥有的单位,同一个进程内的线程共享进程的资源
   (3)线程是处理器调度的基本单位,但进程不是.


3.多线程有几种实现方法,都是什么?
   两种,一种是继承Thread类来实现;另一种是实现Runable接口。


4.多线程同步和互斥有几种实现方法,都是什么?、
  多线程同步主要是:分别是synchronized 和 wait与notify ;
  互斥也就是将资源顺序的排列一下,按照一定的顺序去访问,它使用 synchronized 关键字来实现,
  具体方法是:a.Object obj = new Object(); public void  execute(){synchronized(ojb){//do what you want ...}}


5.多线程同步和互斥有何异同,在什么情况下分别使用他们?举例说明。

  都是共同去访问资源,只是同步访问的时候,资源的访问是无序的,而互斥的时候,资源是有序的。

    

   无意间看了看、写上、

贴一个售票的线程:





int ticket_count = 0;

public TicketOffice() {

}

public TicketOffice(int ticket_count) {
super();
this.ticket_count = ticket_count;
}

public int getTicket_count(){
return ticket_count;
}

public void setTicket_count(int ticket_count){
this.ticket_count = ticket_count; 
}

/**
* sale tickets.
*/
public void sale_tickets0(){
ticket_count--;
if(ticket_count >= 0){  
            System.out.println("sale succeed ,there are : " + ticket_count +" tickets.");  
        }else{  
            System.out.println("there are no tickets !");  
        }
}

/**
* sale tickets.
*/
Object obj = new Object();
public void sale_tickets1(){
ticket_count--;
synchronized (obj) {
if(ticket_count >= 0){  
System.out.println("sale succeed ,there are : " + ticket_count +" tickets.");  
}else{  
System.out.println("there are no tickets !");  
}

}

/**
* sale 
* @param ticket_count
*/
public void sale0(int ticket_count){
final TicketOffice office = new TicketOffice(ticket_count);
for (int i = 0; i <5 ; i++) {
new Thread(new Runnable() {
@Override
public void run() {
while (office.getTicket_count()>0) {
office.sale_tickets0();
}
}
}).start();
}
}


/**
* sale 
* @param ticket_count
*/

public void sale1(int ticket_count){
final TicketOffice office = new TicketOffice(ticket_count);
for (int i = 0; i <5 ; i++) {
new Thread(new Runnable() {
@Override
public void run() {
while (office.getTicket_count()>0) {
office.sale_tickets1();
}
}
}).start();
}
}

public static void main(String[] args) {
TicketOffice office = new TicketOffice();
//office.sale0(100);
office.sale1(100);
}


以下是他人写的:生产者——>消费者


/**
* 内部类模拟一个消息队列,生产者和消费者就去操作这个消息队列
*/
private static class MessageQueue {
private String[] messages;// 放置消息的数据结构
private int opIndex; // 将要操作的位置索引


public MessageQueue(int size) {
if (size <= 0) {
throw new IllegalArgumentException("消息队列的长度至少为1!");
}
messages = new String[size];
opIndex = 0;
}

public synchronized void put(String message) {
// Java中存在线程假醒的情况,此处用while而不是用if!可以参考Java规范!
while (opIndex == messages.length) {
// 消息队列已满,生产者需要等待
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
messages[opIndex] = message;
opIndex++;
System.out.println("生产者 " + Thread.currentThread().getName()+ " 生产了一条消息: " + message);
// 生产后,对消费者进行唤醒
notifyAll();
}


public synchronized String get() {
// Java中存在线程假醒的情况,此处用while而不是用if!可以参考Java规范!
while (opIndex == 0) {
// 消息队列无消息,消费者需要等待
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
String message = messages[opIndex - 1];
opIndex--;
System.out.println("消费者 " + Thread.currentThread().getName()+ " 消费了一条消息: " + message);
// 消费后,对生产者进行唤醒
notifyAll();
return message;
}
}

/**
* test.
* @param args
*/
public static void main(String[] args) {


final MessageQueue message = new MessageQueue(10);
// 创建3个生产者,生产者负责生产消息
for (int p = 0; p < 3; p++) {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {

message.put("消息来了!");

// 生产消息后,休息100毫秒
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "Producer" + p).start();
}


// 创建3个消费者,消费者负责查看消息
for (int s = 0; s < 3; s++) {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {

message.get();
// 消费消息后,休息100毫秒
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "Consumer" + s).start();
}
}


0 0
原创粉丝点击