终于把JAVA这次的作业赶好了...
一半是自己改编的 ,一半是百度的
no!!!
这次是多线程的作业,下次大作业就死定了! 还有算法分析的论文 T T 靠靠靠靠
附上自己改编的JAVA T T 虽然还是有点问题
但是现在也没时间改了,以后再改
就是读写锁的问题
//Article.java
public class Article
{
int size;//文章总数
int number=0;
int i=0;
boolean available = false;
public Article(int size)
{
this.size=size;
}
// Writer 修改文章
public synchronized void write()
{
number=number+1;
System.out.println("Writer writes articles");
}
public void finishwrite()
{
number=number-1;
if(number+i<size)
available = true;
}
public synchronized void read()
{
if(available==true)
{
System.out.println("Reader reads articles");
i=i+1;
}
}
public void finishread()
{
i--;
}
}
//Reader.java
class Reader implements Runnable
{
Article article;
public Reader(Article article)
{
this.article = article;
}
@Override
public void run()
{
article.read();
article.finishread();
}
}
//Writer.java
class Writer extends Thread
{
Article article;
public Writer(Article article)
{
this.article = article;
}
@Override
public void run()
{
article.write();
article.finishwrite();
}
}
//test.java
class Writer extends Thread
{
Article article;
public Writer(Article article)
{
this.article = article;
}
@Override
public void run()
{
article.write();
article.finishwrite();
}
}
虽然对JAVA有很多地方都不懂,但是觉得还是比C、C++要友好