十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
import java.util.*;
创新互联建站专业为企业提供蛟河网站建设、蛟河做网站、蛟河网站设计、蛟河网站制作等企业网站建设、网页设计与制作、蛟河企业网站模板建站服务,十年蛟河做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
class ThreadTest {
static int type = 4, num = 10; //定义资源数目和线程数目
static int[] resource = new int[type]; //系统资源总数
//static int[] copyResource = new int[type]; //副本
static Random rand = new Random();
static Bank[] bank = new Bank[num]; //线程组
Bank temp = new Bank();
public void init() {
//初始化组中每个线程,随机填充系统资源总数
for(int i = 0; i type; i++)
resource[i] = rand.nextInt(10) + 80;
System.out.print("Resource:");
for(int i = 0; i type; i++)
System.out.print(" " + resource[i]);
System.out.println("");
for(int i = 0; i bank.length; i++)
bank[i] = new Bank("#" + i);
}
public ThreadTest4() {
init();
}
class Bank extends Thread {
//银行家算法避免死锁
public int[]
max = new int[type], //总共需求量
need = new int[type], //尚需资源量
allocation = new int[type]; //已分配量
private int[]
request = new int[type], //申请资源量
copyResource = new int[type]; //资源副本
private boolean isFinish = false; //线程是否完成
int[][] table = new int[bank.length][type*4]; //二维资源分配表
private void init() {
// 随机填充总共、尚需、已分配量
synchronized(resource) {
for(int i = 0; i type; i++) {
max[i] = rand.nextInt(5) + 10;
need[i] = rand.nextInt(10);
allocation[i] = max[i] - need[i];
resource[i] -= allocation[i]; //从系统资源中减去已分配的
}
printer();
for(int i = 0; i type; i++) {
if(resource[i] 0) {
//若出现已分配量超出系统资源总数的错误则退出
System.out.println("The summation of Threads' allocations is out of range!");
System.exit(1);
}
}
}
}
public Bank(String s) {
setName(s);
init();
start();
}
public Bank() {
//none
}
public void run() {
try {
sleep(rand.nextInt(2000));
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
while(true) {
//程序没有完成时一直不断申请资源
if(askFor() == false) {
try {
sleep(1000);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
}
else
tryRequest();
if(noNeed() == true)
break;
}
//休眠一段时间模拟程序运行
try {
sleep(1000);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(getName() + " finish!");
synchronized(resource) {
//运行结束释放占有资源
for(int i = 0; i type; i++) {
resource[i] += allocation[i];
need[i] = allocation[i] = max[i] = 0;
}
}
}
private void printer() {
//打印当前资源信息
System.out.print(getName() + " Max:");
for(int i = 0; i type; i++)
System.out.print(" " + max[i]);
System.out.print(" Allocation:");
for(int i = 0; i type; i++)
System.out.print(" " + allocation[i]);
System.out.print(" Need:");
for(int i = 0; i type; i++)
System.out.print(" " + need[i]);
System.out.print(" Available:");
for(int i = 0; i type; i++)
System.out.print(" " + resource[i]);
System.out.println("");
}
private boolean askFor() {
//随机产生申请资源量并检测是否超标
boolean canAsk = false;
for(int i = 0; i type; i++) {
request[i] = rand.nextInt(20);
//防止申请量超过所需量
if(request[i] need[i])
request[i] = need[i];
}
for(int i = 0; i type; i++) //防止随机申请资源全为0
if(request[i] 0)
canAsk = true;
synchronized(resource) {
//锁住可供资源检查是否超标
for(int i = 0; i type; i++) {
if(request[i] resource[i])
//如果申请资源超过可供资源则等待一段时间后重新申请
return false;
}
}
return canAsk;
}
private void tryRequest() {
//创建副本尝试分配请求
synchronized(resource) {
for(int i = 0; i type; i++)
//依然要防止请求量超出范围
if(request[i] resource[i])
return;
for(int i = 0; i type; i++) {
//复制资源量并减去需求量到一个副本上
copyResource[i] = resource[i];
copyResource[i] -= request[i];
}
System.out.print(getName() + " ask for:");
for(int i = 0; i type; i++)
System.out.print(" " + request[i]);
System.out.println("");
if(checkSafe() == true) {
//如果检查安全则将副本值赋给资源量并修改占有量和需求量
for(int i = 0; i type; i++) {
resource[i] = copyResource[i];
allocation[i] += request[i];
need[i] -= request[i];
}
System.out.println(getName() + " request succeed!");
}
else
System.out.println(getName() + " request fail!");
}
}
private boolean checkSafe() {
//银行家算法检查安全性
synchronized(bank) {
//将线程资源信息放入二维资源分配表检查安全性,0~type可用资源/type~type*2所需资源/type*2~type*3占有资源/type*3~-1可用+占用资源
for(int i = 0; i bank.length; i++) {
for(int j = type; j type*2; j++) {
table[i][j] = bank[i].need[j%type];
}
for(int j = type*2; j type*3; j++) {
table[i][j] = bank[i].allocation[j%type];
}
}
//冒泡排序按需求资源从小到大排
for(int i = 0; i bank.length; i++) {
for(int j = i; j bank.length-1; j++) {
sort(j, 4);
}
}
//进行此时刻的安全性检查
for(int i = 0; i type; i++) {
table[0][i] = copyResource[i];
table[0][i+type*3] = table[0][i] + table[0][i+type*2];
if(table[0][i+type*3] table[1][i+type])
return false;
}
for(int j = 1; j bank.length-1; j++) {
for(int k = 0; k type; k++) {
table[j][k] = table[j-1][k+type*3];
table[j][k+type*3] = table[j][k] + table[j][k+type*2];
if(table[j][k+type*3] table[j+1][k+type])
return false;
}
}
}
return true;
}
private void sort(int j, int k) {
//递归冒泡排序
int tempNum;
if(table[j][k] table[j+1][k]) {
for(int i = type; i type*2; i++) {
tempNum = table[j][i];
table[j][i] = table[j+1][i];
table[j+1][i] = tempNum;
}
/*temp = bank[j];
bank[j] = bank[j+1];
bank[j+1] = temp;*/
}
else if(table[j][k] == table[j+1][k] k type*2) //此资源量相同时递归下一个资源量排序并且防止超出范围
sort(j, k+1);
}
private boolean noNeed() {
//是否还需要资源
boolean finish = true;
for(int i = 0; i type; i++) {
if(need[i] != 0) {
finish = false;
break;
}
}
return finish;
}
}
public static void main(String[] args) {
ThreadTest t = new ThreadTest();
//后台线程,设定程序运行多长时间后自动结束
new Timeout(30000, "---Stop!!!---");
}
}
package temp;
import sun.misc.Sort;
/**
* @author zengjl
* @version 1.0
* @since 2007-08-22
* @Des java几种基本排序方法
*/
/**
* SortUtil:排序方法
* 关于对排序方法的选择:这告诉我们,什么时候用什么排序最好。当人们渴望先知道排在前面的是谁时,
* 我们用选择排序;当我们不断拿到新的数并想保持已有的数始终有序时,我们用插入排序;当给出的数
* 列已经比较有序,只需要小幅度的调整一下时,我们用冒泡排序。
*/
public class SortUtil extends Sort {
/**
* 插入排序法
* @param data
* @Des 插入排序(Insertion Sort)是,每次从数列中取一个还没有取出过的数,并按照大小关系插入到已经取出的数中使得已经取出的数仍然有序。
*/
public int[] insertSort(int[] data) {
1/11页
int temp;
for (int i = 1; i data.length; i++) {
for (int j = i; (j 0) (data[j] data[j - 1]); j--) {
swap(data, j, j - 1);
}
}
return data;
}
/**
* 冒泡排序法
* @param data
* @return
* @Des 冒泡排序(Bubble Sort)分为若干趟进行,每一趟排序从前往后比较每两个相邻的元素的大小(因此一趟排序要比较n-1对位置相邻的数)并在
* 每次发现前面的那个数比紧接它后的数大时交换位置;进行足够多趟直到某一趟跑完后发现这一趟没有进行任何交换操作(最坏情况下要跑n-1趟,
* 这种情况在最小的数位于给定数列的最后面时发生)。事实上,在第一趟冒泡结束后,最后面那个数肯定是最大的了,于是第二次只需要对前面n-1
* 个数排序,这又将把这n-1个数中最小的数放到整个数列的倒数第二个位置。这样下去,冒泡排序第i趟结束后后面i个数都已经到位了,第i+1趟实
* 际上只考虑前n-i个数(需要的比较次数比前面所说的n-1要小)。这相当于用数学归纳法证明了冒泡排序的正确性
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import oracle.jdbc.driver.OracleDriver;
public class GenPaperServlet extends HttpServlet
{
Connection conn;
Statement stmt;
ResultSet rs;
int total_question_num;
int total_question_in_paper;
int total_paper_num;
String curr_classid;
public GenPaperServlet()
{
conn = null;
stmt = null;
rs = null;
total_question_num = 0;
total_question_in_paper = 0;
total_paper_num = 0;
curr_classid = "";
}
public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
httpservletresponse.setContentType("text/html;charset=GBK");
PrintWriter printwriter = httpservletresponse.getWriter();
printwriter.println("htmlhead/headbodycenter");
printwriter.println("请以POST方式提交");
printwriter.println("/center/body/html");
printwriter.close();
public class Excer{
public static void main(String args[])
{
Excer ex=new Excer();
int x=0;
int y=0;
ex.math(x,y);
}
void math(int x,int y){
MyMath mm = new MyMath();
System.out.println("x="+x+" ,y="+y);
System.out.println("x+y="+mm.plus(x,y));
System.out.println("x-y="+mm.minus(x,y));
System.out.println("x*y="+mm.multi(x,y));
System.out.println("x/y="+mm.div(x,y));
}
}
class MyMath
{
int plus(int a,int b)
{
return(a+b);
}
int minus(int a,int b)
{
return(a-b);
}
int multi(int a,int b)
{
return(a*b);
}
float div(int a,int b)
{
return ((float)a/b);
}
}