zoukankan      html  css  js  c++  java
  • 12月

    终于把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++要友好

  • 相关阅读:
    Leetcode 笔记 110
    Leetcode 笔记 100
    Leetcode 笔记 99
    Leetcode 笔记 98
    Leetcode 笔记 101
    Leetcode 笔记 36
    Leetcode 笔记 35
    Leetcode 笔记 117
    Leetcode 笔记 116
    android加载速度优化,通过项目的优化过程分析
  • 原文地址:https://www.cnblogs.com/shmilykk/p/2830306.html
Copyright © 2011-2022 走看看