📄 multythreadtest.java
字号:
/*
* Created on 2004-11-30
*
* TODO To change the template for this generated file go to Window -
* Preferences - Java - Code Style - Code Templates
*/
package com.nay0648.ds.bplustree;
import java.io.*;
import java.sql.*;
import java.util.*;
/**
* Description:<br>
*    this is thread operate B+ tree
* @abstract
* @keywords
* @author nay0648<br>
* if you have any questions,advices,suggests,or find any
* bugs,please mail me: <a href="mailto:">nay0648@sina.com</a>
* @version last modified:Dec 2, 2004
*/
class TestThread extends Thread
{
private static final int RANGE=10000;//the range of random number
private static final int SEARCH=0;
private static final int LINEAR_SEARCH=1;
private static final int INSERT=2;
private static final int DELETE=3;
private BPlusTree tree;//the tree
private int iteration;//the number of task each thread do
private List value;//value inserted into the tree
/**
* @param index
* thread's index
* @param group
* thread group
* @param tree
* the tree
* @param iteration
* the number of task each thread do
* @param value
* value inserted into the tree
*/
public TestThread(int index,ThreadGroup group,BPlusTree tree,int iteration,List value)
{
super(group,Integer.toString(index));
this.tree=tree;
this.iteration=iteration;
this.value=value;
this.start();
}
public void run()
{
long res;
int i,op,rvali;
Integer val;
for(i=0;i<iteration;i++)
{
try
{
op=(int)(Math.random()*4);//choose a ramdom operation
switch(op)
{
case SEARCH:
{
if(value.size()>0)
{
//choose a ramdom value to search
val=(Integer)value.get((int)(Math.random()*value.size()));
res=tree.search(val);
System.out.println("I am thread "+this.getName()+",I search "+val+",get: "+res);
}
}break;
case LINEAR_SEARCH:
{
if(value.size()>0)
{
//choose a ramdom value to search
val=(Integer)value.get((int)(Math.random()*value.size()));
res=tree.linearSearch(val);
System.out.println("I am thread "+this.getName()+",I linear search "+val+",get: "+res);
}
}break;
case INSERT:
{
val=new Integer((int)(Math.random()*RANGE));//generate a random number to insert
tree.insert(val,val.longValue());//here use keyword value as file pointer value
value.add(val);
System.out.println("I am thread "+this.getName()+",I inserted "+val);
}break;
case DELETE:
{
if(value.size()>0)
{
//choose a random value to delete
val=(Integer)value.remove((int)(Math.random()*value.size()));
res=tree.delete(val);
System.out.println("I am thread "+this.getName()+",I deleted "+val+" get: "+res);
}
}break;
}
}
/*
* because there are no synchronization to make sure each thread
* get the value from the List synchronized,so when thread stopen
* between value.size() and value.get() operation,it will cause
* problem,but it is not important
*/
catch(IndexOutOfBoundsException exc)
{}
catch(java.lang.IllegalArgumentException exc1)
{
System.err.println(exc1.getMessage());
}
catch(IOException exc2)
{
exc2.printStackTrace();
}
}
}
}
/**
* Description:<br>
*    this is all thread's monitor
* @abstract
* @keywords
* @author nay0648<br>
* if you have any questions,advices,suggests,or find any
* bugs,please mail me: <a href="mailto:">nay0648@sina.com</a>
* @version last modified:Dec 2, 2004
*/
class Monitor extends Thread
{
private BPlusTree tree=null;//the tree
private ThreadGroup sub;//thread group
private String path;//tree path
private List value;//value inserted in tree
/**
* @param rank
* tree rank
* @param path
* tree path
* @param thread
* thread number
* @param iteration
* task number of each thread
*/
public Monitor(int rank,String path,int thread,int iteration)
{
int i;
TestThread tt;
try
{
this.path=path;
tree=new BPlusTree(rank,Types.INTEGER,path);//build a integer tree
sub=new ThreadGroup("monitor");
value=new LinkedList();
for(i=0;i<thread;i++)tt=new TestThread(i,sub,tree,iteration,value);
this.start();
}
catch(IOException exc)
{
exc.printStackTrace();
}
}
/**
* @param path
* a already exists tree's path
* @param thread
* thread number
* @param iteration
* task number of each thread
*/
public Monitor(String path,int thread,int iteration)
{
int i;
TestThread tt;
try
{
this.path=path;
tree=new BPlusTree(path);
sub=new ThreadGroup("monitor");
value=new LinkedList();
for(i=0;i<thread;i++)tt=new TestThread(i,sub,tree,iteration,value);
this.start();
}
catch(IOException exc)
{
exc.printStackTrace();
}
}
public void run()
{
int i;
try
{
for(;;)
{
Thread.sleep(200);
System.out.println("I am monitor,still left: "+sub.activeCount());
//all thread's task finished
if(sub.activeCount()==0)
{
tree.close();
System.out.println("complete.");
System.out.println("---------------------------------");
tree=new BPlusTree(path);//open the tree again
tree.printTree();
System.out.println("---------------------------------");
System.out.println(tree);
System.out.println("---------------------------------");
System.out.println("search:");
for(i=0;i<value.size();i++)
System.out.print(tree.search(value.get(i))+" ");
System.out.print("\n");
System.out.println("---------------------------------");
System.out.println("linear search:");
for(i=0;i<value.size();i++)
System.out.print(tree.linearSearch(value.get(i))+" ");
System.out.print("\n");
break;
}
}
}
catch(Exception exc)
{
exc.printStackTrace();
}
finally
{
try
{
if(tree!=null) tree.close();
}
catch(IOException exc2)
{
exc2.printStackTrace();
}
}
}
}
/**
* Description:<br>
*    this is the demo of how to use B+ tree implemention.
* in this demo,it first create a new tree,then create some thread
* to do varies operation to the tree,at last print the tree's
* information
* @abstract
* @keywords
* @author nay0648<br>
* if you have any questions,advices,suggests,or find any
* bugs,please mail me: <a href="mailto:">nay0648@sina.com</a>
* @version last modified:Dec 2, 2004
*/
public class MultyThreadTest
{
public static void showHelp()
{
System.out.println("######################################################################\n"+
"# this is the demo of how to use B+ tree implemention.in this demo #\n"+
"# it first build a new integer tree,then create some thread to do v- #\n"+
"# aries operation to the tree,at last print the tree's information. #\n"+
"######################################################################");
System.out.println("usage:");
System.out.println("java -jar bplustree.jar [tree rank] [tree file path] [thread number] [each thread's task number]");
System.out.println("if you use Linux operating system,[thread number] must equal to 1.");
}
public static void main(String args[]) throws IOException
{
File path;
Monitor monitor;
if(args.length!=4) showHelp();
else
{
path=new File(args[1]);
if(path.exists()) path.delete();
monitor=new Monitor(Integer.valueOf(args[0]).intValue(),args[1],Integer.valueOf(args[2]).intValue(),Integer.valueOf(args[3]).intValue());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -