1.==============================ABC3Threads============================
package test;
public class ABC3Threads {
static boolean taStarted=false;
static boolean tbStarted=false;
static boolean tcStarted=false;
public static void main(String[] args) {
try {
int i=0;
while(i++<10){
taStarted=false;
tbStarted=false;
tcStarted=false;
Object oA=new Object();
Object oB=new Object();
Object oC=new Object();
OutputThread ta=new OutputThread(oC, oA,"A");
OutputThread tb=new OutputThread(oA, oB,"B");
OutputThread tc=new OutputThread(oB, oC,"C");
ta.start();
// Thread.sleep(3);
while(!taStarted){
Thread.sleep(1);
}
tb.start();
while(!tbStarted){
Thread.sleep(1);
}
//Thread.sleep(3);
tc.start();
//Thread.sleep(1000);
while(ta.isAlive()){
Thread.sleep(1);
}
System.out.println("");
// Thread.sleep(3111);
// ta.interrupt();
// tb.interrupt();
// tc.interrupt();
ta.stop();
tb.stop();
tc.stop();
}
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class OutputThread extends Thread{
Object obj1;
Object obj2;
String name;
int i=10;
public OutputThread(Object o1,Object o2,String name) {
obj1=o1;
obj2=o2;
this.name=name;
}
public void run() {
while(i-->0){
//System.out.println("i===================="+i);
synchronized (obj1) {
synchronized (obj2) {
try {
System.out.print(name);
if(name.equals("A")&&i==9){
ABC3Threads.taStarted=true;
}
if(name.equals("B")&&i==9){
ABC3Threads.tbStarted=true;
}
if(name.equals("C")&&i==9){
ABC3Threads.tcStarted=true;
}
obj2.notify();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
obj1.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
2.==============================ABCD4Threads============================
package test;
public class ABCD4Threads {
public static void main(String[] args) {
try {
Object a=new Object();
Object b=new Object();
Object c=new Object();
Object d=new Object();
ABCD4T ta=new ABCD4T(d, a, "A");
ABCD4T tb=new ABCD4T(a, b, "B");
ABCD4T tc=new ABCD4T(b, c, "C");
ABCD4T td=new ABCD4T(c, d, "D");
ta.start();
Thread.sleep(50);
tb.start();
Thread.sleep(50);
tc.start();
Thread.sleep(50);
td.start();
//System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ABCD4T extends Thread {
Object p1;
Object c;
String name;
public ABCD4T(Object p1,Object c, String name) {
super(name);
this.p1 = p1;
this.c = c;
this.name = name;
}
int i=10;
public void run() {
try {
while(i-->0){
String namemmm=this.getName();
synchronized (p1) {
synchronized (c) {
System.out.print(name);
c.notify();
}
p1.wait();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.==============================PutOneGetOne============================
package test;
public class PutOneGetOne {
String objg ;
public static void main(String[] args) {
try {
quangKu s=new quangKu();
F ft=new F(s);
Q qt=new Q(s);
s.setFQ(ft, qt);
ft.start();
Thread.sleep(10);
qt.start();
} catch (Exception e) {
e.printStackTrace();
}
System.exit(0);
}
}
class quangKu{
F f;
Q q;
public void setFQ(F f,Q q) {
this.f=f;
this.q=q;
}
String objg ;
public void put(String obj){
try {
synchronized (f) {
synchronized (q) {
objg=obj;
System.out.println("put:"+objg);
q.notify();
}
f.wait();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void get(){
try {
synchronized (q) {
synchronized (f) {
System.out.println("get:"+objg);
f.notify();
}
q.wait();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class F extends Thread{
quangKu s;
public F(quangKu s) {
this.s=s;
}
int i=10;
public void run() {
try {
while(i-->0){
s.put(i+"");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Q extends Thread{
quangKu s;
public Q(quangKu s) {
this.s=s;
}
int i=10;
public void run() {
while(i-->0){
s.get();
}
}
}
4.==============================JiCheng============================
package test;
public class JiCheng {
public static void main(String[] args) {
try {
System.out.println(jicheng(100000));
} catch (Exception e) {
e.printStackTrace();
}
}
private static String jicheng(int n)throws Exception{
int weiShu=1;
//System.out.println(Integer.MAX_VALUE);
int []result=new int[21474836];
result[0]=1;
while(n>1){
int jinWei=0;
for(int i=0;i<weiShu;i++){
int temp=result[i]*n+jinWei;
int geWei=temp%10;
jinWei=temp/10;
result[i]=geWei;
}
//==process jinWei
while(jinWei>0){
int nextGeWei=jinWei%10;
result[weiShu++]=nextGeWei;
jinWei/=10;
}
n--;
}
//=======================process result
String res="";
for(int i=weiShu-1;i>=0;i--){
res+=result[i];
}
return res;
}
}
分享到:
相关推荐
据结构算法 shu ju jie gou suan fa : C++Builder 6.0程序集
通过一个典型的测试函数,测试遗传算法设计的好坏
基本算法fenxi , 附带源码, 采用模板,C++语言描述
此代码是函数文件,只需调用即可求得结果。建立一个矩阵,再把矩阵带入函数
android 游戏开发基础,相关suanfa的介绍,经典.....
zui quanmiandematlab suan fa
yi chuang suan fa yi
c常用算法源代码,一些基本的算法,毕竟这个比较基础, 很有用,值得
zhe shi FFT suan fa xiang guan FPGA dai ma
music suan fa,its very useful, MUSIC methods of DOA estimates. Signal for broadband signal source..
zai xue xi fpga yao liaojie yi xie suan fa shi hen tou tong ,zhe ge dui chu xuezhe yinggai you bangzhu
里面是数据结构中的希尔排序算法,有兴趣的看下,很不错的。
该文件包含了一般的FCM聚类算法和改进的FCM聚类算法
C语言结构化实现贪心算法,利用贪心策略解决背包问题。现有载重为M公斤的背包和n种货物。第i种货物的重量为Wi,它的总价值为Pi,假定M、Wi、Pi均为整数。设计程序给出装货方法,使装入背包的货物总价值达到最大
这是关于汽车牌照自动识别系统的算法设计的源程序,上传上来给大家分享,希望能有帮助
自己编的遗传算法程序,我周围朋友都说好用,传上来大家分享,直接解压缩,无须密码
文件中包含各种图像处理源代码,包括二值化处理的源代码,对图像进行直方图均衡处理的源代码,对图像进行中值滤波处理(图像复原)的源代码,对图像进行均值滤波处理(图像复原)的源代码对图像进行对角镜像处理...
关于计算共形几何的Jupyter注释 如何开始 要查看注释,只需在终端中发出以下命令 在bash中: . hello jupyter notebook 或在zsh中: . ./hello jupyter notebook
插值法 cha zhi fa de kengc wen kang mei shen me yong de
仿真实现无线传感器网络定位算法-MATLAB源代码