Interview3--->智胜

本文探讨了Java中线程访问同步与非同步方法的行为,通过实例演示了线程如何交互并行执行非同步方法。同时,深入解析了数据库连接池的工作原理,包括其初始化过程、连接分配与释放机制,以及最小和最大连接数设置的影响。

1、当一个线程进入一个对象的同步方法,其他线程能否访问该对象的其他非同步方法?

答:只要不是同步方法,就不要等对象锁,所以不管这个对象是否有其它线程锁定了,在其它线程访问非同步方法都不需要等同步锁的动作。

package com.fruitking.test;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Student {
private String name = "fruitking";
private String consume = "fruitking is a superman!";
private String from = "Home";
private String major = "computer science & technology";
public synchronized String getName(){
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"-->"+this.name);
try{
Thread.currentThread().sleep(5000);
}catch(Exception e){
e.printStackTrace();
}
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"-->>"+this.name);
return name;
}
public synchronized String getConsume() {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"-->"+this.consume);
try{
Thread.currentThread().sleep(3000);
}catch(Exception e){
e.printStackTrace();
}
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"-->>"+this.consume);
return consume;
}
public String getFrom() {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"-->"+this.from);
return from;
}
public String getMajor() {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"-->"+this.major);
try{
Thread.currentThread().sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"-->>"+this.major);
return major;
}
}
package com.fruitking.test;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ThreadA implements Runnable {
private Student student;
public void run() {
String str = student.getFrom() + " ";
str = str + student.getName() + " ";
str = str + student.getMajor() + " ";
str = str + student.getConsume() + " ";
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+" "+ str);
}
public void setStudent(Student student){
this.student = student;
}
}
package com.fruitking.test;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ThreadB implements Runnable {
private Student student;
public void run() {
String str = student.getFrom() + " ";
str = str + student.getConsume() + " ";
str = str + student.getMajor() + " ";
str = str + student.getName() + " ";
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+" "+ str);
}
public void setStudent(Student student){
this.student = student;
}
}
package com.fruitking.test;
public class TestA {
/**
* @param args
*/
public static void main(String[] args) {
Student student = new Student();
ThreadA threadA = new ThreadA();
threadA.setStudent(student);
Thread thread1 = new Thread(threadA);
ThreadB threadB = new ThreadB();
threadB.setStudent(student);
Thread thread2 = new Thread(threadB);
thread1.start();
thread2.start();
}
}
运行结果:(这个结果不是唯一的,但是其意义是不变的)
2009-10-15 11:32:038-->Thread-1-->China
2009-10-15 11:32:038-->Thread-1-->fruitking is a superman!
2009-10-15 11:32:038-->Thread-0-->China
2009-10-15 11:32:041-->Thread-1-->>fruitking is a superman!
2009-10-15 11:32:041-->Thread-1-->computer science & technology
2009-10-15 11:32:041-->Thread-0-->fruitking
2009-10-15 11:32:042-->Thread-1-->>computer science & technology
2009-10-15 11:32:046-->Thread-0-->>fruitking
2009-10-15 11:32:046-->Thread-0-->computer science & technology
2009-10-15 11:32:046-->Thread-1-->fruitking
2009-10-15 11:32:047-->Thread-0-->>computer science & technology
2009-10-15 11:32:051-->Thread-1-->>fruitking
2009-10-15 11:32:051-->Thread-1 China fruitking is a superman! computer science & technology fruitking
2009-10-15 11:32:051-->Thread-0-->fruitking is a superman!
2009-10-15 11:32:054-->Thread-0-->>fruitking is a superman!
2009-10-15 11:32:054-->Thread-0 China fruitking computer science & technology fruitking is a superman!

2、请描述数据连接池的运行机制?

数据库连接是一种关键的有限的昂贵的资源,这一点在多用户的网页应用程序中体现得尤为突出。对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标。数据库连接池正是针对这个问题提出来的。
数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而再不是重新建立一个;释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏。这项技术能明显提高对数据库操作的性能。
数据库连接池在初始化时将创建一定数量的数据库连接放到连接池中,这些数据库连接的数量是由最小数据库连接数来设定的。无论这些数据库连接是否被 使用,连接池都将一直保证至少拥有这么多的连接数量。连接池的最大数据库连接数量限定了这个连接池能占有的最大连接数,当应用程序向连接池请求的连接数超 过最大连接数量时,这些请求将被加入到等待队列中。
数据库连接池的最小连接数和最大连接数的设置要考虑到下列几个因素:
1) 最小连接数是连接池一直保持的数据库连接,所以如果应用程序对数据库连接的使用量不大,将会有大量的数据库连接资源被浪费;
2) 最大连接数是连接池能申请的最大连接数,如果数据库连接请求超过此数,后面的数据库连接请求将被加入到等待队列中,这会影响之后的数据库操作。
3) 如果最小连接数与最大连接数相差太大,那么最先的连接请求将会获利,之后超过最小连接数量的连接请求等价于建立一个新的数据库连接。不过,这些大于最小连接数的数据库连接在使用完不会马上被释放,它将被放到连接池中等待重复使用或是空闲超时后被释放。
--------------------------------------------------------分割线-----------------------------------------------------------------
J2EE服务器启动时会建立一定数量的池连接,并一直维持不少于此数目的池连接。
调用:客户端程序需要连接时,池驱动程序会返回一个未使用的池连接并将其表记为 忙。如果当前没有空闲连接,池驱动程序就新建一定数量的连接,新建连接的数量有配置参数决定。
释放:当使用的池连接调用完成后,池驱动程序将此连接表记为空闲, 其他调用就可以使用这个连接。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值