小编Rah*_*ran的帖子

不变性和单例是否解决了相同的目的?

我读到的是不变性意味着一旦分配的值保持不变,即一旦分配,它在代码中反映为相同的值。

大多数情况下,不变性用于多线程环境中的函数式编程范式。

但。

单例模式是否也解决了同样的目的,

请在下面找到用java编写的单例,

 package com.main;

class Student{
  private Student(){

  }
  public static Student INSTANCE; 
  static{
    INSTANCE = new Student();
  }

  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Override
  public String toString(){     
    return "Student is "+this.name;
  }
}


public class SingletonTest {

  /**
   * @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Student student = Student.INSTANCE;
    student.setName("Arnold");

    student = Student.INSTANCE;
    student.setName("Kevin"); …
Run Code Online (Sandbox Code Playgroud)

javascript java singleton design-patterns immutability

4
推荐指数
1
解决办法
4719
查看次数

Thread.sleep方法永久停止线程

class SelfishRunner extends Thread{
     private int tick = 1;
     private int num ;
     public     SelfishRunner(int x){
             this.num = x;
     }
     @Override
     public void run(){
          try{
               while(tick < 400000){
                  Thread.sleep(250);
                  if((tick%50000) == 0){
                       System.out.println(" Thread# "+num+","+Thread.currentThread().getName()+", tick "+tick);
                       }
                  tick++;
              }
            }catch(Exception e){
                   System.out.println(e);
             }
         }
    }


    public class RaceDemo{
          private final static int NUMRUNNERS = 2;  
          public static void main(String[] args){
               SelfishRunner[] runners = new SelfishRunner[NUMRUNNERS];
               for(int x=0,y=1; x < NUMRUNNERS; x++){
                       runners[x] = new SelfishRunner(x);
                       runners[x].setPriority(y++);
               }
               runners[0].setName("JEEPERS");
               runners[1].setName("KREEPERS"); …
Run Code Online (Sandbox Code Playgroud)

java multithreading thread-safety

2
推荐指数
1
解决办法
379
查看次数

将两个数相乘而不使用乘法

我有一个测试,我需要编码以便在不使用乘法的情况下将两个数相乘,

代码如下,

function multiply(num,toNum){
  var product = 0;
  for(var i = 1; i <= toNum; i++){
    product += num;
  }

  return product;
}

console.log(multiply(2,5)); 
Run Code Online (Sandbox Code Playgroud)

输出是

rahul@rahul:~/myPractise/Algo$ node MultiplyWithoutLoop.js 
10 
rahul@rahul:~/myPractise/Algo$ 
Run Code Online (Sandbox Code Playgroud)

以上代码是否令人满意或需要是否有改进的余地.

可以应用更好的逻辑.

嘿,

我用递归解决了它,

这是代码,

function multiply01(num,toNum){
  var product = num;    
  return (toNum >= 1) ? product + multiply01(product,--toNum) : 0; 
}
Run Code Online (Sandbox Code Playgroud)

javascript logic data-structures

1
推荐指数
1
解决办法
2285
查看次数

使用Explicit Lock来避免死锁会产生异常

我在这里尝试做的是有一个共享资源,即SharedResource777.java,这个类有两个方法doIt()和setBFlag(),这两个线程获取Lock并使用线程执行方法.

代码如下,

    import java.util.concurrent.locks.*;

    class SharedResource777{
        private boolean bFLag = false;
        private Lock lockObj = new ReentrantLock();
        private Condition condition = lockObj.newCondition();

        public void doIt() {        
            try{
                lockObj.lock();         
                while(!bFLag){
                    System.out.println(" THE THREAD "+Thread.currentThread().getName());            
                    condition.wait();
                }           
            }catch(Exception e){
                System.out.println(e);
            }finally{
                lockObj.unlock();
            }           
        }

        public void setBFlag(boolean bFLag){        
            try{
                lockObj.lock(); 
                this.bFLag = bFLag;     
                System.out.println(" THE THREAD "+Thread.currentThread().getName()+" ["+this.bFLag+"]");
                condition.signal();
            }catch(Exception e){
                System.out.println(e);
            }finally{
                lockObj.unlock();
            }
        }

    }

    class MyThread620 extends Thread{

        private SharedResource777 resource;

        MyThread620(String threadName,SharedResource777 resource){
            super(threadName);
            this.resource = resource;
        } …
Run Code Online (Sandbox Code Playgroud)

java multithreading thread-safety

0
推荐指数
1
解决办法
216
查看次数

从列表中删除项目

我从学生的ArrayList中删除一个Student对象,这是我的代码,下面是Student.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package corejava.list;

/**
 * 
 * @author Rahul 
 */

public class Student {
private int id;
private String name;

public Student(int id,String name){
    this.id = id;
    this.name = name;
}

public Student(int id){
    this.id = id;        
}

@Override
public int hashCode(){
    return this.getId() * 37;
}

@Override
public String toString(){
    StringBuffer strb = new StringBuffer();
    strb.append("\tID : ").append(this.getId()).append(", NAME : ").append(this.getName()); …
Run Code Online (Sandbox Code Playgroud)

java collections

0
推荐指数
1
解决办法
174
查看次数